LoginSignup
0
1

More than 3 years have passed since last update.

WebViewでカスタムURLを動作させるには

Posted at

WebViewでカスタムURLを動作させるには

Xamarin.FormsのWebViewを使ったアプリ内でHTMLを開いた際に、JavaScriptのwindow.openでGoogleMapsアプリが開かなかったので調べてみた。
同じHTMLをSafariで開いた際に、カスタムURLが動作し、GoogleMaps等の別アプリが起動するようになっている前提とする。

WebViewを継承させたRendererを使う

参考:Xamarin.Forms(iOS)でjavascriptのwindow.open()を動作させるには(英語)
以下抜粋。window.open実行時に下記メソッドが呼ばれる。
URLがカスタムURL形式になっていればアプリが起動する。

MyWebViewRenderer.cs
[Export("webView:createWebViewWithConfiguration:forNavigationAction:windowFeatures:")]
public WKWebView CreateWebView(WKWebView webView,WKWebViewConfiguration configuration,WKNavigationAction action, WKWindowFeatures  features)
{
    if (action.Request.Url != null)
    {
        NSUrl url = action.Request.Url;
        UIApplication.SharedApplication.OpenUrl(url);
    }
    return null;
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1