How to know if the URL requested on WKWebView is finished loading?

koby1koby1 Member ✭✭
edited December 2018 in Xamarin.iOS

The login of my app is through web so I would open it via WKWebView, my problem is I need to know if the login page has been loaded successfully. After that I want to get the JSON response from my login page and save the tokens on my device

Tagged:

Answers

  • ColeXColeX Member, Xamarin Team Xamurai
    edited December 2018

    You can detect the url in the method DecidePolicy and do your stuff in DidFinishNavigation

    //assign delegate
    webview.NavigationDelegate = new MyWKNavigationDelegate();
    
    //custom delegate
    class MyWKNavigationDelegate : WKNavigationDelegate
    {
        bool isLogged = false;
    
        public override void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
        {
            base.DidFinishNavigation(webView, navigation);
    
            if (isLogged)
            {
                //do the stuff 
            }
        }
    
        public override void DecidePolicy(WKWebView webView, WKNavigationAction navigationAction, Action<WKNavigationActionPolicy> decisionHandler)
        {
            if(navigationAction.Request.Url.AbsoluteString == "specific login string")
            {
                isLogged = true;
            }
            else
            {
                isLogged = false;
            }
        }
    }
    
  • koby1koby1 Member ✭✭

    @ColeX tried your answer but didn't quite get it. I wanted to add or show the WKWebView on my view if the url requested is finished loading.

  • ColeXColeX Member, Xamarin Team Xamurai

    The login of my app is through web

    What do you mean by through web ? web request? UIWebview?

  • koby1koby1 Member ✭✭
    edited December 2018

    @ColeX through web. I'll load the login url on the WKWebView. What I wanted to do is to show a splash screen or some loading screen first, then if the url is finished loading then that will be the time I will be adding the WKWebView on my current View

Sign In or Register to comment.