我有 3 个 URL,我使用WKWebView
.
这些 URL 用于触发 SSO 进入另一个产品,使用我的应用在登录时设置的 cookie。
它们看起来如下
店铺
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
当我打开任何选项卡时,视图会正确加载。但是,如果我先打开个人资料或商店,则主页会呈现我最后打开的任何视图,商店或个人资料。
我怀疑这可能与 URL 相同减去中继状态参数有关,因为将主页 url 更改为https://google.com
总是有效的东西,我在个人资料和商店之间没有任何问题。
我正在使用一个单例实例WKProcessPool
来共享 cookie,我可以通过在de init
webview 中的打印看到它在被解除后被销毁。
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
,我没有对路径或参数做任何事情,也许这是问题所在?