After ios 11 upgrade WKWebView does not load my website.

After ios 11 upgrade WKWebView does not load my website.
I added the following methods to my renderer:

    [Export("webViewWebContentProcessDidTerminate:")]
    public virtual void ContentProcessDidTerminate(WKWebView webView)
    {

        Console.WriteLine("ContentProcessDidTerminate");
    }

    [Export("webView:didFinishNavigation:")]
    public void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
    {

        UIApplication.SharedApplication.NetworkActivityIndicatorVisible = false;
        Console.WriteLine("DidFinishNavigation");
    }

    [Export("webView:didFailNavigation:withError:")]
    public void DidFailNavigation(WKWebView webView, WKNavigation navigation, NSError error)
    {
        // If navigation fails, this gets called
        Console.WriteLine("DidFailNavigation:" + error.ToString());
    }

    [Export("webView:didFailProvisionalNavigation:withError:")]
    public void DidFailProvisionalNavigation(WKWebView webView, WKNavigation navigation, NSError error)
    {
        // If navigation fails, this gets called
        Console.WriteLine("DidFailProvisionalNavigation" + error.ToString());
    }

    [Export("webView:didStartProvisionalNavigation:")]
    public void DidStartProvisionalNavigation(WKWebView webView, WKNavigation navigation)
    {

        UIApplication.SharedApplication.NetworkActivityIndicatorVisible = true;
        // When navigation starts, this gets called
        Console.WriteLine("DidStartProvisionalNavigation");
    }

The application shows didStartProvisionalNavigation but it does not move to the other methods.

I also added the following to my info.plist:
NSAppTransportSecurity

NSAllowsArbitraryLoads

I also tried running my WKWebview against another of my sites and that one works just fine.

In Safari, after attaching it to my WKWebView, I can see that some webapi calls does not seem to complete the request. Yet in some cases the same request does complete but some others does not.

Is anyone having this type of issue?

Any help to solve this would be really appreciated.

Thanks,

Hector

Tagged:

Posts

  • MichaelRumplerMichaelRumpler ATMember ✭✭✭✭✭

    You could try with Xamarin.iOS 11.0. My WKWebView crashed before and works with the new X.i. It's a different bug, but may be related. See bug 59247.

  • JohnHardmanJohnHardman GBUniversity mod

    @HectorMeneses - Do you have NSAllowsArbitraryLoadsInWebContent in your info.plist?

  • HectorMenesesHectorMeneses USMember ✭✭

    Thanks Michael,

    I actually found the issue for my case. It turns out that in iOS 11 the WKWebView is trying to navigate to "about:blank". I'm not aware of our site having that "page".

    Then in my DecidePolicy that page was not allowed and I guess that was also stopping my website from continuing loading normally. I added a condition to enable navigation to "about:blank" and that solved it.

        [Export("webView:decidePolicyForNavigationAction:decisionHandler:")]
        public override void DecidePolicy(WKWebView webView, WKNavigationAction navigationAction, Action<WKNavigationActionPolicy> decisionHandler)
        {
    
            var navType = navigationAction.NavigationType;
            var targetFrame = navigationAction.TargetFrame;
    
            var url = navigationAction.Request.Url;
            if (
                url.ToString().StartsWith("http") && (targetFrame != null && targetFrame.MainFrame == true)
                )
            {
                decisionHandler(WKNavigationActionPolicy.Allow);
            }
            else if (
                (url.ToString().StartsWith("http") && targetFrame == null)
                ||
                url.ToString().StartsWith("mailto:")
                || url.ToString().StartsWith("tel:")) //Whatever your test happens to be
            {
                //decisionHandler(WKNavigationActionPolicy.Allow);
    
                Device.BeginInvokeOnMainThread(() =>
                {
                    UIApplication.SharedApplication.OpenUrl(url);
                }
                );
            } // NEED THIS IN IOS 11
            else if ( url.ToString().StartsWith("about") )
            {
                decisionHandler(WKNavigationActionPolicy.Allow);
            }
        }
    
  • JongfooSheeJongfooShee MYUniversity ✭✭

    Hi Hector Meneses, where should i put DecidePolicy? In WebViewRenderer? If I put override in my WebViewRenderer, it will display "no suitable method found to override"?

  • HectorMenesesHectorMeneses USMember ✭✭

    Hi Jongfoo Shee,

    You need to put it under:
    public class WebNavigationDelegate : WKNavigationDelegate
    {

    [Export("webView:decidePolicyForNavigationAction:decisionHandler:")]
    public override void DecidePolicy(WKWebView webView, WKNavigationAction navigationAction, Action decisionHandler)
    {
    ....
    }
    }

  • AdrianGhiAdrianGhi USMember ✭✭

    @HectorMeneses thank you for your solution! :smile:

Sign In or Register to comment.