在将用户重定向到WKWebView的链接时遇到一些麻烦。
用户本质上是指使用WKWebView在网站上进行身份验证,该网站会检查身份验证,然后将用户重定向回我们的应用程序。
我最初的想法是允许用户在重定向过程中退出应用程序存在某种限制。这就是为什么我实现了WKNavigationDelegate并且看到当我们尝试验证下面的代码时,didReceive挑战运行在第二个if情况下 - 我只假设它是一件好事。虽然该应用程序没有被重定向。
以下是我的代码。
import UIKit
import WebKit
class WebViewViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.isHidden = false
tabBarController?.tabBar.isHidden = true
webView.navigationDelegate = self
if let url = URL(string: "SomeUrl") {
let request = URLRequest(url: url)
webView.load(request)
}
}
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewWillAppear(_ animated: Bool) {
self.navigationController?.navigationBar.isHidden = false
}
override func viewWillDisappear(_ animated: Bool) {
self.tabBarController?.tabBar.isHidden = false
}
func webViewDidFinishLoad(_ webView: UIWebView) {
if let URL = webView.request?.url {
OAuthHelper.oAuthManager.processOAuthStep1Response(url: URL)
}
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
}
func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
}
func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
if challenge.previousFailureCount > 0 {
completionHandler(Foundation.URLSession.AuthChallengeDisposition.cancelAuthenticationChallenge, nil)
} else if let serverTrust = challenge.protectionSpace.serverTrust {
completionHandler(Foundation.URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: serverTrust))
} else {
print("unknown state. error: \(challenge.error)")
// do something w/ completionHandler here
}
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if (navigationAction.navigationType == .linkActivated){
decisionHandler(.cancel)
} else {
decisionHandler(.allow)
}
}
}
答案 0 :(得分:0)
我没有完全理解这个问题。如果问题是如果您希望用户从Webview重定向到Native App,那么您确实有不同的选项。我将提供选项。如果您正在查看,请告诉我。