From UIWebView to WKWebView

Hi,
In my app I display an HTML page with some links on it. Some of the links scroll the page down to a corresponding paragraph.
This used to work with UIWebView but not with the new WKWebView;
Here is my old code:

    _webView = new UIWebView ();
    _webView.AutoresizingMask = UIViewAutoresizing.All;
     _webView.Frame = new CGRect (0, 0, View.Frame.Width, View.Frame.Height);
    View.AddSubview (_webView);
     _webView.LoadRequest(new NSUrlRequest(new NSUrl(_webPageAddress)));
     _webView.ScalesPageToFit = false;
    _webView.ShouldStartLoad = HandleShouldStartLoad;
    ...

    bool HandleShouldStartLoad(UIWebView webView, NSUrlRequest request, UIWebViewNavigationType navigationType)
    {
     // Filter out clicked link
    if (navigationType == UIWebViewNavigationType.LinkClicked)
    {
       if (UIApplication.SharedApplication.CanOpenUrl(request.Url))
       {
           // Open in Safari instead
            UIApplication.SharedApplication.OpenUrl(request.Url);
            return false;
         }
      }
     return true;
    }

With the new WKWebView I succeed in showing the Web page but I cannot make it scroll when I click a link.
Here is my new code:

    _webView = new WKWebView(View.Frame, new WKWebViewConfiguration());
    _webView.AutoresizingMask = UIViewAutoresizing.All;
    _webView.Frame = new CGRect (0, 0, View.Frame.Width, View.Frame.Height);
    _webView.NavigationDelegate = new MyDelegate();
     View.AddSubview (_webView);
    _webView.LoadRequest(new NSUrlRequest(new NSUrl(_webPageAddress)));
     ...

    public class MyDelegate : WKNavigationDelegate
    {
       public override void DecidePolicy(WKWebView webView, WKNavigationAction navigationAction, 
         System.Action<WKNavigationActionPolicy> decisionHandler)
        {

                var url = navigationAction.Request.Url;
                if (navigationAction.NavigationType == WKNavigationType.LinkActivated)
                {
                    webView.BeginInvokeOnMainThread(() =>
                    {
                        UIApplication.SharedApplication.OpenUrl(url);
                    }
                    );
               }
         decisionHandler?.Invoke(WKNavigationActionPolicy.Allow);
    }

how can I make the Web page scroll when I click on a link?
Thank you!

Tagged:
Sign In or Register to comment.