How to delete all cookies of UIWebView?

IosIphoneObjective CUiwebviewUiwebviewdelegate

Ios Problem Overview


In my application, I have a UIWebview that loads linkedin auth page for login. When user logs in, cookies saves into the application.

My app has a logout button that is not related to linkedin login. So when user clicks on this button, he logs off from the app. I want that this log off will clear his linkedin cookies also from the app, so that user will log out completely.

Ios Solutions


Solution 1 - Ios

According to [this question][1], you can go through each cookie in the "Cookie Jar" and delete them, like so:

NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in [storage cookies]) {
   [storage deleteCookie:cookie];
}
[[NSUserDefaults standardUserDefaults] synchronize];

[1]: https://stackoverflow.com/questions/771498/where-are-an-uiwebviews-cookies-stored "this question"

Solution 2 - Ios

Just wanted to add some info regarding this.

In OS X 10.9/iOS 7 and later, you can use -resetWithCompletionHandler: to clear the cookies and cache etc. of the whole app from your sharedSession:

> Empties all cookies, caches and credential stores, removes disk files, > flushes in-progress downloads to disk, and ensures that future > requests occur on a new socket.

[[NSURLSession sharedSession] resetWithCompletionHandler:^{
    // Do something once it's done.
}];

The for-In loop with deleteCookie: sounds like modifying while enumerating a collection to me. (Don't know, could be a bad idea?)

Solution 3 - Ios

You could make a function inside the html of the WebView, that cleans the cookies.

If you need the cleaning to be done only once you could trigger this function with a Titanium event, only when the app starts.

Solution 4 - Ios

If anyone is looking for Swift Solution:

    let storage = HTTPCookieStorage.shared
    if let cookies = storage.cookies{
        for cookie in cookies {
             storage.deleteCookie(cookie)
        }
    }

Solution 5 - Ios

Previous answers didn't help me in the case of using MKWebView. So, I found another solution:

func loadAuthUrl(_ url: URL) {
        
        let finally: VoidClosure = { [weak self] in
            guard let self = self else { return }
            let request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 60.0)
            self.webView.load(request)
        }
        
        let cookieStore = webView.configuration.websiteDataStore.httpCookieStore
        cookieStore.getAllCookies({ [weak self] cookies in
            guard let _ = self else { return }
            let instagramCookies = cookies.filter({ $0.domain == ".instagram.com" })
            if instagramCookies.isEmpty {
                finally()
            } else {
                DispatchQueue.global().async(execute: {
                    let group = DispatchGroup()
                    for cookie in cookies {
                        group.enter()
                        cookieStore.delete(cookie, completionHandler: { group.leave() })
                    }
                    group.wait()
                    DispatchQueue.main.async(execute: finally)
                })
            }
        })
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVaibhav SaranView Question on Stackoverflow
Solution 1 - IosSergio MouraView Answer on Stackoverflow
Solution 2 - IosCaiView Answer on Stackoverflow
Solution 3 - Iosjack kallisView Answer on Stackoverflow
Solution 4 - IosSaleh Enam ShohagView Answer on Stackoverflow
Solution 5 - IosRiosixxView Answer on Stackoverflow