我正在使用uiwebview的uiviewController。在此uiwebview中,用户必须登录,成功登录后,它应将用户重定向到应用程序。但是就我而言,它留在uiwebview中,并没有将我重新定向回到该应用程序。
我尝试在API委托中使用此代码:
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
DispatchQueue.main.async {
if (url.scheme == "go-back"){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "WebView") as! ViewController
self.window?.rootViewController!.present(controller, animated: true, completion: { () -> Void in
})
}
}
return true
}
并创建了一个URL方案,但它不起作用。
step1:
使用WKWebView
而不是UIWebView
在您的ViewController中。
step2:
使您的ViewController成为WKWebView
webView.navigationDelegate = self
webView.uiDelegate = self
step3:
最终在您的ViewController
WKNavigationDelegate
extension ViewController : WKNavigationDelegate{
func webView(
_ webView: WKWebView,
decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
guard let url = navigationAction.request.url else {
decisionHandler(.allow)
return
}
//now u and ur server team can decide on what url will they redirect and what will be url string on login success
//lets say u and ur server team decides url to be https://some_base_url/login/success
if url.absoluteString.contains("/login/success") {
// this means login successful
decisionHandler(.cancel)
_ = self.navigationController?.popViewController(animated: false)
}
else {
decisionHandler(.allow)
}
}
}
编辑:
当OP的代码上有问题,以解决问题
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate{
var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string: "https://")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
func webView(
_ webView: WKWebView,
decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
guard let url = navigationAction.request.url else {
decisionHandler(.allow)
return
}
if url.absoluteString.contains("/login/success") {
// this means login successful
decisionHandler(.cancel)
_ = self.navigationController?.popViewController(animated: false)
}
else {
decisionHandler(.allow)
}
}
}
//
导入Uikit导入webkit
类ViewController:UiviewController,Wkuidelegate,WKNAvigationDelegate {
var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string: "https://")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}}func webView(
_ webView: WKWebView,
decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
guard let url = navigationAction.request.url else {
decisionHandler(.allow)
return
}
if url.absoluteString.contains("/login/success") {
// this means login successful
decisionHandler(.cancel)
_ = self.navigationController?.popViewController(animated: false)
}
else {
decisionHandler(.allow)
}
}
我通过使用此行遇到了错误:
if url.absoluteString.contains("/login/success") {
// this means login successful
decisionHandler(.cancel)
_ = self.navigationController?.popViewController(animated: false)
}
说使用未解决的标识符" self"