WebView 退出时注销帐户/清除导航

Log out of accounts / Clear navigation when WebView is resigned

我在我公司的应用程序中实现了一个WebView,这个WebView 是用户在webApp 上登录自己的帐户的女仆,这样他就可以完成一个不能在App 本身中执行的任务。

我遇到的问题是:我在WebView中登录了一个账户,一旦我在App中更改了用户账户,WebView中显示的账户仍然是我第一次登录的账户,这是一个问题,因为所有每次关闭 WebView 时都应该退出 WebView 的内容 VC.

这是我的代码:

class DashboardWebView: UIViewController, WKNavigationDelegate {

// MARK: - Outlets

@IBOutlet var webView: WKWebView?
    
// MARK: - Public

var url: URL?
var shouldAutoLogin = false
var shouldRefetch: (() -> Void)?

// MARK: - View life cycle

override func viewDidLoad() {
    super.viewDidLoad()
    webView?.navigationDelegate = self
    title = R.string.localizable.dashboard_webview_title().uppercased()
    setNavigationBarStyle(style: NavigationBarStyle.gradientWhiteNoBorder)
    navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(close))
    
    if let url = url, let webView = webView {
        webView.load(URLRequest(url: url))
    }
}

override func viewWillDisappear(_ animated: Bool) {
    shouldRefetch?()
}

func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
    print(error)
}

func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
    if shouldAutoLogin == true {
        // This is maid to log in the right Environement, but are not credentials of a user account
        let user = "user"
        let password = "passWOrd!"
        let credential = URLCredential(user: user, password: password, persistence: URLCredential.Persistence.none)
        challenge.sender?.use(credential, for: challenge)
        completionHandler(URLSession.AuthChallengeDisposition.useCredential, credential)
    }
}

// MARK: -  Methods

@objc private func close() {
    self.navigationController?.dismiss(animated: true)
}

}

webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)

是用来过稳定环境的,这里的凭据不是账号的,我在评论这个方法的时候也遇到了同样的问题

我的问题: 可以有一个私人导航,每次我 kill/instantiate WebView ?

删除所有参数

提前致谢

您可以use/modify以下扩展来根据您的要求清理网络数据和 cookie。它应该适用于您的情况。

extension WKWebView {

    class func clean(completion : @escaping () -> Void) {
        
        guard #available(iOS 9.0, *) else {return}

        HTTPCookieStorage.shared.removeCookies(since: Date.distantPast)
        let dispatch_group = DispatchGroup()
        WKWebsiteDataStore.default().fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { records in
            records.forEach { record in
                dispatch_group.enter()
                WKWebsiteDataStore.default().removeData(ofTypes: record.dataTypes, for: [record], completionHandler: {
                    dispatch_group.leave()
                })
                #if DEBUG
                    print("WKWebsiteDataStore record deleted:", record)
                #endif
            }
            dispatch_group.notify(queue: DispatchQueue.main) {
                completion() //
            }
        }
    }
}

尝试一下,如果遇到任何问题请告诉我。