Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete cookies UIWebView

How to delete cookies in UIWebView? This code:

NSArray* cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
for (NSHTTPCookie *cookie in cookies) {
 [cookies deleteCookie:cookie];
}

deletes cookies but when I restart my application, there are same cookies in NSHTTPCookieStorage. Sometimes this code works, but i want to make it work everytime.How to solve this problem?

like image 242
Timur Mustafaev Avatar asked Sep 27 '11 09:09

Timur Mustafaev


People also ask

What is UIWebView on Apple Iphone?

A view that embeds web content in your app.

Is UIWebView deprecated?

As of iOS 12.0, 'UIWebView' has been explicitly marked as deprecated. Apple is encouraging developers to adopt WKWebView instead, which was introduced in iOS 8.0.

Does Webview share cookies with Safari?

WKWebView is an in-app browser that displays web content. It doesn't share cookies or web site data with other WKWebView instances, or with the Safari browser.


2 Answers

Try something like this:

NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
        NSArray* facebookCookies = [cookies cookiesForURL:
                                    [NSURL URLWithString:@"http://login.facebook.com"]];
        for (NSHTTPCookie* cookie in facebookCookies) {
            [cookies deleteCookie:cookie];
        }
like image 146
LightNight Avatar answered Sep 21 '22 00:09

LightNight


This worked for me:

NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];

NSArray *allCookies = [cookies cookies];

for(NSHTTPCookie *cookie in allCookies) {
    if([[cookie domain] rangeOfString:@"facebook.com"].location != NSNotFound) {
        [cookies deleteCookie:cookie];
    }
}
like image 42
Benjamin Oman Avatar answered Sep 23 '22 00:09

Benjamin Oman