Cannot open links without target=_blank tag in them.

Hi there. Please excuse my newbie question, I am a very new Xamarin developer. I found some resources in Swift that were similar to this issue, but I do not know Swift, so was unable to translate the code into a xamarin solution. I was unable to get any link to work in a webpage viewed within the WKWebView that my app uses. Now, I can open links that have the "target=_blank" attribute, but I am unable to open any other link, specifically, links that open within the current window, and links that have the "mailto:" and "tel" attributes in the URL. I got the target=_blank links to work by adding the following class to the WebViewController, and a line to the ViewDidLoad method:

public class NavDelegate : WKUIDelegate
{

    public override WKWebView CreateWebView(WKWebView webView, WKWebViewConfiguration configuration, WKNavigationAction navigationAction, WKWindowFeatures windowFeatures)
    {
        var url = navigationAction.Request.Url;
        if (navigationAction.TargetFrame == null)
        {
            webView.LoadRequest(navigationAction.Request);
        }


        return null;
    }
}

Here is the line added to the ViewDidLoad() method:
WebAppView.UIDelegate = new NavDelegate();

And the complete ViewDidLoad method:
public override void ViewDidLoad()
{
base.ViewDidLoad();
WebViewLogo.Owner = this;

        //Check System Version and set WebView accordingly
        //WKWebView only supported on iOS Versions 11+
        if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
        {
            View.Frame = new CoreGraphics.CGRect(0, 108, 1024, 611);
            View.AddConstraint(NSLayoutConstraint.Create(View, NSLayoutAttribute.Height, NSLayoutRelation.Equal, View, NSLayoutAttribute.Height, 1.0f, 0.0f));
            View.AddConstraint(NSLayoutConstraint.Create(View, NSLayoutAttribute.Width, NSLayoutRelation.Equal, View, NSLayoutAttribute.Width, 1.0f, 0.0f));
            View.AddConstraint(NSLayoutConstraint.Create(View, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, View, NSLayoutAttribute.Trailing, 1.0f, 0.0f));
            View.AddConstraint(NSLayoutConstraint.Create(View, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, View, NSLayoutAttribute.Bottom, 1.0f, 43.0f));

            var WebAppView = new WKWebView(View.Frame, new WKWebViewConfiguration());
            WebAppView.UIDelegate = new NavDelegate();
            View.AddSubview(WebAppView);
            //set the margins and constraints


            // Add a tap recognizer to reset timer
            var actionGestureRecognizer = new UITapGestureRecognizer(this, new ObjCRuntime.Selector("HandleActionEvent:"));
            actionGestureRecognizer.NumberOfTapsRequired = 1;
            actionGestureRecognizer.Delegate = new GestureDelegate();
            WebAppView.AddGestureRecognizer(actionGestureRecognizer);

            // Load the quote
            //Attempt to get the current quote if active
            var storedQuoteDataAsJson = QuoteManager.FetchQuote();

            // If quote is empty or expired pop to the contact screen
            if (String.IsNullOrEmpty(storedQuoteDataAsJson) || !QuoteManager.IsQuoteActive((int)NSUserDefaults.StandardUserDefaults.IntForKey("QuoteExpireTimeInSeconds")))
            {
                AppDelegate.Self.PopToTarget(ControllerScreen.ContactController);
            }

            // Load the quote
            Quote currentQuote = QuoteManager.DeserializeQuote(storedQuoteDataAsJson);
            if (currentQuote != null)
            {
                // NOTE: https required for iOS 9 ATS
                var url = String.Format("{0}{1}", NSUserDefaults.StandardUserDefaults.StringForKey("WebURL"), currentQuote.QuoteID);
                WebAppView.LoadRequest(new NSUrlRequest(new NSUrl(url)));
            }
        }
        else //UIWebView is not supported on iOS versions less than 11, so must use UIWebView
        {

            View.Frame = new CoreGraphics.CGRect(0, 108, 1024, 611);
            View.AddConstraint(NSLayoutConstraint.Create(View, NSLayoutAttribute.Height, NSLayoutRelation.Equal, View, NSLayoutAttribute.Height, 1.0f, 0.0f));
            View.AddConstraint(NSLayoutConstraint.Create(View, NSLayoutAttribute.Width, NSLayoutRelation.Equal, View, NSLayoutAttribute.Width, 1.0f, 0.0f));
            View.AddConstraint(NSLayoutConstraint.Create(View, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, View, NSLayoutAttribute.Trailing, 1.0f, 0.0f));
            View.AddConstraint(NSLayoutConstraint.Create(View, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, View, NSLayoutAttribute.Bottom, 1.0f, 43.0f));
            var WebAppView = new UIWebView(View.Frame);
            View.AddSubview(WebAppView);
            //set the margins and constraints

            // Add a tap recognizer to reset timer
            var actionGestureRecognizer = new UITapGestureRecognizer(this, new ObjCRuntime.Selector("HandleActionEvent:"));
            actionGestureRecognizer.NumberOfTapsRequired = 1;
            actionGestureRecognizer.Delegate = new GestureDelegate();
            WebAppView.AddGestureRecognizer(actionGestureRecognizer);

            // Load the quote
            //Attempt to get the current quote if active
            var storedQuoteDataAsJson = QuoteManager.FetchQuote();

            // If quote is empty or expired pop to the contact screen
            if (String.IsNullOrEmpty(storedQuoteDataAsJson) || !QuoteManager.IsQuoteActive((int)NSUserDefaults.StandardUserDefaults.IntForKey("QuoteExpireTimeInSeconds")))
            {
                AppDelegate.Self.PopToTarget(ControllerScreen.ContactController);
            }

            // Load the quote
            Quote currentQuote = QuoteManager.DeserializeQuote(storedQuoteDataAsJson);
            if (currentQuote != null)
            {
                // NOTE: https required for iOS 9 ATS
                var url = String.Format("{0}{1}", NSUserDefaults.StandardUserDefaults.StringForKey("WebURL"), currentQuote.QuoteID);
                WebAppView.LoadRequest(new NSUrlRequest(new NSUrl(url)));

            }
        }
    }

Any help would be much appreciated. Thanks in advance.

Sign In or Register to comment.