How to delete all cookies of UIWebView?

天大地大妈咪最大 提交于 2019-11-26 06:59:42

问题


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.


回答1:


According to this question, 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];



回答2:


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?)




回答3:


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.




回答4:


If anyone is looking for Swift Solution:

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


来源:https://stackoverflow.com/questions/4471629/how-to-delete-all-cookies-of-uiwebview

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