Search code examples
iosobjective-ciphonexcodewkwebview

How to redirect from a button click inside WKWebview and to go back to the iOS application using objective c?


I am developing a native iOS application . On a button click I am loading a website app inside the WKWebview. Inside the web view after logging in with username and password the following permission screen comes to us which has "Allow" and "Deny" button.

What I want is when pressing on the "Allow" button the current screen should get dismissed and redirect back to my application. Similarly with the "Deny" button . After so much of research I found the following Swift code for the WKWebviewDelegate:

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {

    // here we handle internally the callback url and call method that call handleOpenURL (not app scheme used)
    if let url = navigationAction.request.url, url.host == "localhost" , url.path == "/square-oauth-callback" {


        print(url)
        print(url.valueOf("code"))
        //decisionHandler(.cancel)

        /*  Dismiss your view controller as normal
            And proceed with OAuth authorization code
            The code you receive here is not the auth token; For auth token you have to make another api call with the code that you received here and you can procced further
         */

        /*
            Auth Process Flow: https://developer.squareup.com/docs/oauth-api/how-it-works#oauth-access-token-management

            Obtain Auth Token: https://developer.squareup.com/reference/square/oauth-api/obtain-token
         */

    }
    decisionHandler(.allow)
}

Am I correct in my approach? If yes can someone please tell the Objective-C alternative of the above code? Sorry for the case as I recently started my career using Swift and I have very little knowledge about Objective-C. If no, can someone suggest me the correct approach?


Solution

  • The same delegate method can be used in objective-C also:

        - (void)webView:(WKWebView *)webView 
    decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction 
    decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;
    

    Objective-C conversion of above code:

      - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
    {
            if ([navigationAction.request.URL isEqual:@"localhost"] && [navigationAction.request.URL.path isEqual:@"/square-oauth-callback"]) {
                decisionHandler(WKNavigationActionPolicyCancel);
                NSLog(@"URL:%@",navigationAction.request.URL);
            }
            else
            {
                decisionHandler(WKNavigationActionPolicyAllow);
            }
    }
    

    Apple Documentation: https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455641-webview?language=objc