繁体   English   中英

如何使用目标 c 从 WKWebview 内的按钮单击重定向到 go 回到 iOS 应用程序?

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

我正在开发一个原生 iOS 应用程序。 单击按钮时,我正在WKWebview中加载网站应用程序。 使用用户名和密码登录后,在 web 视图中,出现以下权限屏幕,其中包含“允许”和“拒绝”按钮。

我想要的是当按下“允许”按钮时,当前屏幕应该被关闭并重定向回我的应用程序。 与“拒绝”按钮类似。 经过大量研究,我发现了 WKWebviewDelegate 的以下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)
}

我的方法正确吗? 如果是,有人可以告诉上面代码的 Objective-C 替代方案吗? 抱歉,我最近开始使用 Swift 开始我的职业生涯,而我对 Objective-C 知之甚少。 如果没有,有人可以建议我正确的方法吗?

同样的委托方法也可以在 objective-C 中使用:

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

Objective-C 转换上述代码:

  - (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);
        }
}

苹果文档: https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455641-webview?language=objc

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM