1
votes

I am developing a xamarin forms cross-plotform application, I am using webview to load an external html page into it, Now we have an anchor tag in that html page, Here how to open that link in device browser when user clicks on it for both iOS and Android, i have write following block of code in AppDelegate.cs file for iOS but it is not working

 private bool HandleShouldStartLoad(UIWebView webview,NSUrlRequest request,UIWebViewNavigationType navtype)
    {
        if(navtype==UIWebViewNavigationType.LinkClicked)
        {
            UIApplication.SharedApplication.OpenUrl(request.Url);
            return false;
        }  
        return true;
    }

Please Help How to achieve it.

Thanks in Advance

2
You can just use Device.OpenUri()Sasha
I am loading webview with external Html page, we have anchor tag in that html page, here when we click on on that anchor i need open it in browser not in app? is that possible with Device.OpenUri()srinivas challa
Yes, that is possible.Sasha
i am loading my webview like Loadfromweb.Source = new UrlWebViewSource { Url = myurl}; can you pleas explain how i need to change heresrinivas challa
replace UIApplication.SharedApplication.OpenUrl(request.Url); with Device.OpenUri(New Uri(request.Url));Sasha

2 Answers

5
votes

I think you can try to achieve this by using a WebViewRenderer. Create a WebView like MyWebView in forms and implement renderers on each platform.

On iOS:

[assembly: ExportRenderer(typeof(MyWebView), typeof(MyWebViewRenderer))]
namespace OpenUriDemo.iOS
{
    public class MyWebViewRenderer : WebViewRenderer
    {
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            if (NativeView != null)
            {
                ((UIWebView)NativeView).Delegate = new MyWebViewDelegate();
            }
        }
    }

    public class MyWebViewDelegate : UIWebViewDelegate
    {
        public override bool ShouldStartLoad(UIWebView webView, NSUrlRequest request, UIWebViewNavigationType navigationType)
        {
            if (navigationType == UIWebViewNavigationType.LinkClicked)
            {
                UIApplication.SharedApplication.OpenUrl(request.Url);
                return false;
            }
            return true;
        }
    }
}

On Android:

[assembly: ExportRenderer(typeof(MyWebView), typeof(MyWebViewRenderer))]
namespace OpenUriDemo.Droid
{
    public class MyWebViewRenderer : WebViewRenderer
    {
        public MyWebViewRenderer(Context context) : base(context)
        {

        }
        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
        {
            base.OnElementChanged(e);
            Control.SetWebViewClient(new MyWebViewClient());
        }
    }

    public class MyWebViewClient : WebViewClient
    {
        public override bool ShouldOverrideUrlLoading(Android.Webkit.WebView view, string url)
        {
            if (url != "Your First Request Url")
            {
                Intent i = new Intent(Intent.ActionView);
                i.SetData(Android.Net.Uri.Parse(url));
                Xamarin.Forms.Forms.Context.StartActivity(i);
                return false;
            }
            return true;
        }
    }
}

Use MyWebView to load your url in Forms.

-1
votes

Use Device.OpenUri and pass your URL.

Device.OpenUri(new Uri(request.Url));

You can use UIApplication.SharedApplication.OpenUrl, but you should create a new NSUrl object, passing in your URL.

UIApplication.SharedApplication.OpenUrl(new NSUrl(request.Url));