WKWebView not detecting URL is different

余生颓废 提交于 2021-01-28 06:34:21

问题


I have 3 URL's I open within my app using a WKWebView.

These URL's are used to trigger an SSO into another product, using a cookie set by my app on login.

They look as follows

shop

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

profile

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

home

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

When I open any tabs, the view loads correctly. If I open profile or shop first though, home renders whichever view I was on last, shop or profile.

I suspect it is perhaps something todo with the URL being the same minus the relay state param, as changing the home url to something like https://google.com always works and I do not have any issues going between profile and shop.

I am using a singleton instance of WKProcessPool to share cookies and I can see by printing in de init the webview is being destroyed once it is dismissed.

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 { }

My singleton WKProcessPool

class ProcessPool {
    static let shared = WKProcessPool()
}

The URL's come from an API as they are, I simply store them as URL on the model using Codable, I am not doing anything with the paths or params, perhaps this is the issue?

来源:https://stackoverflow.com/questions/55952802/wkwebview-not-detecting-url-is-different

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!