Search code examples
iosswiftwebviewuiviewcontrollerwkwebview

Log out of accounts / Clear navigation when WebView is resigned


I am implementing a WebView in the app of my company, this WebView is maid for the user to log in his own account on the webApp so he can complete a task that can't be performed in the App itself.

The problem I have is: I log in an account in the WebView, once I change user account in the App, the account displayed in the WebView will still be the one I first logged in which is a problem because all content of the WebView should be resigned every time I close the WebView VC.

Here my code:

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)

Is used to pass the stable environement, the credentials here are not the ones of an account, also I have the same problem when commenting this method.

My question: It is possible to have either a private navigation, to delete all the parameters every time I kill/instantiate the WebView ?

Thank you in advance


Solution

  • You can use/modify the following extension to clean web data and cookies as per your requirement. It should work in your case.

    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() //
                }
            }
        }
    }
    

    Try it and let me know if you face any issue.