![](/img/trans.png)
[英]How to send data from WKWebview to HTML file using evaluateJavaScript | iOS | Objective-C
[英]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.