如何在UIWebView中成功登录后重定向回应用程序

时间:2017-09-18 12:05:19

标签: ios swift uiviewcontroller uiwebview uistoryboard

我正在使用带有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方案,但它无法正常工作。

2 个答案:

答案 0 :(得分:1)

第1步:

在ViewController中使用WKWebView而不是UIWebView

第2步:

让您的ViewController成为WKWebView

的代表
    webView.navigationDelegate = self
    webView.uiDelegate = self

第3步:

最后在WKNavigationDelegate

中实施ViewController
    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)
        }
    }
}

答案 1 :(得分:0)

_id

导入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)
}

表示使用未解析的标识符" self"