1

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

9
  • You can just use Device.OpenUri() Commented Feb 8, 2018 at 12:26
  • 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() Commented Feb 8, 2018 at 12:34
  • Yes, that is possible. Commented Feb 8, 2018 at 13:38
  • i am loading my webview like Loadfromweb.Source = new UrlWebViewSource { Url = myurl}; can you pleas explain how i need to change here Commented Feb 8, 2018 at 13:52
  • replace UIApplication.SharedApplication.OpenUrl(request.Url); with Device.OpenUri(New Uri(request.Url)); Commented Feb 8, 2018 at 13:53

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

-1

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));

2 Comments

Thanks Balaraju for replying i am loading webview like mywebview.source=new UrlWebViewSource { Url=myurl}; i am getting anchor tag from that myurl into my webview, Can i use your block of code for that
where i need to add UIApplication.SharedApplication.OpenUrl(new NSUrl(request.Url)); this? in portable class library or in iOS project?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.