如何从 WKWebview 中的按钮单击重定向并使用 objective c 返回到 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
的以下 Swift 代码:
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);
}
}
Apple 文档:https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455641-webview?language=objc
我正在开发本机 iOS 应用程序。单击按钮,我正在 WKWebview
中加载一个网站应用程序。在使用用户名和密码登录后的 Web 视图中,我们会看到以下权限屏幕,其中有“允许”和“拒绝”按钮。
我想要的是当按下“允许”按钮时,当前屏幕应该被关闭并重定向回我的应用程序。与“拒绝”按钮类似。经过大量研究后,我发现 WKWebviewDelegate
的以下 Swift 代码:
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);
}
}
Apple 文档:https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455641-webview?language=objc