WKWebView未检测到URL是不同的

时间:2019-05-02 12:46:48

标签: swift webkit wkwebview wkwebviewconfiguration

我使用WKWebView在我的应用程序中打开了3个URL。

这些URL用于通过我的应用在登录时设置的cookie来触发SSO进入另一种产品。

它们看起来如下

商店

https://identity.domain.io/auth/some/params/protocol/saml/clients/sso?RelayState=shop_page

个人资料

https://identity.domain.io/auth/some/params/protocol/saml/clients/sso?RelayState=profile_page

https://identity.domain.io/auth/some/params/protocol/saml/clients/sso

当我打开任何选项卡时,视图将正确加载。但是,如果我先打开个人资料或商店,则home会渲染我上一次在商店或个人资料上显示的视图。

我怀疑这可能与URL减去中继状态参数是相同的,因为将主页URL更改为https://google.com之类的方法总是可行的,而且我在配置文件和商店之间没有任何问题。

我正在使用WKProcessPool的单例实例来共享cookie,并且通过在de init中打印可以看到,一旦关闭该Web视图就被破坏了。

import WebKit

class BaseWKWebViewController: UIViewController {

    var requestURL: URL? {
        didSet {
            guard let requestURL = requestURL else { return }
            loadURL(requestURL)
        }
    }

    private var sessionCookies: [HTTPCookie] {
        var __cookies = [HTTPCookie]()

        webView.configuration.websiteDataStore.httpCookieStore.getAllCookies { cookies in
            __cookies = cookies.filter({ cookie -> Bool in
                cookie.name.contains("IDENTITY") || cookie.name.contains("AUTH_SESSION_ID")
            })
        }

        return __cookies
    }

    lazy var webView: WKWebView = {
        let webConfiguration = WKWebViewConfiguration()
        webConfiguration.dataDetectorTypes = [.all]
        webConfiguration.processPool = ProcessPool.shared
        let webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.navigationDelegate = self
        webView.allowsBackForwardNavigationGestures = false
        return webView
    }()

    private var estimatedProgressObserver: NSKeyValueObservation?
    private let progressView = UIProgressView(progressViewStyle: .default)

    override func loadView() {
        view = webView
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        setupProgressView()
        setupEstimatedProgressObserver()
    }

    func loadURL(_ url: URL) {
        let request = URLRequest(url: url)
        webView.load(URLRequest(url: url), with: sessionCookies)
    }

    private func setupProgressView() {
        progressView.trackTintColor = theme.color(.gray)
        progressView.progressTintColor = theme.color(.primary)
        view.addSubview(progressView)
        progressView.anchor(top: view.topAnchor, leading: view.leadingAnchor, trailing: view.trailingAnchor, size: .init(width: 0, height: 4))
        progressView.isHidden = true
    }

    private func setupEstimatedProgressObserver() {
        estimatedProgressObserver = webView.observe(\.estimatedProgress, options: [.new]) { [weak self] webView, _ in
            self?.progressView.progress = Float(webView.estimatedProgress)
        }
    }
}

extension BaseWKWebViewController: WKNavigationDelegate {

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        if let url = navigationAction.request.url, let host = url.host {
            if host.contains("test.co.uk") || host.contains(configValue(forKey: "APP_DOMAIN")) || host.contains("hub.com") {
                decisionHandler(.allow)
                return
            }

            UIApplication.shared.open(url)
            decisionHandler(.cancel)
        }
    }



    func webView(_: WKWebView, didStartProvisionalNavigation _: WKNavigation!) {
        if progressView.isHidden {
            progressView.isHidden = false
        }

        UIView.animate(withDuration: 0.33, animations: { self.progressView.alpha = 1.0 })
    }

    func webView(_: WKWebView, didFinish _: WKNavigation!) {
        UIView.animate(withDuration: 0.33, animations: { self.progressView.alpha = 0.0 }) { isFinished in
            self.progressView.isHidden = isFinished
        }
    }
}

extension BaseWKWebViewController: Brandable { }

我的单身WKProcessPool

class ProcessPool {
    static let shared = WKProcessPool()
}

URL本身来自API,我只是使用URL将它们存储为Codable在模型上,我对路径或参数没有做任何事情,也许这是问题所在?

0 个答案:

没有答案