Delete cookies UIWebView

前端 未结 5 1615
执笔经年
执笔经年 2020-12-17 02:49

How to delete cookies in UIWebView? This code:

NSArray* cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
for (NSHTTPCookie *cookie in cooki         


        
相关标签:
5条回答
  • 2020-12-17 03:27

    Make sure to call:

    [[NSUserDefaults standardUserDefaults] synchronize];

    in the end... Works like a charm...

    0 讨论(0)
  • 2020-12-17 03:30

    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];
            }
    
    0 讨论(0)
  • 2020-12-17 03:30

    deleting a single cookie does not always work for some odd reason. To truly get a cookie deleted you will need to store the not specific cookie and then reload them and then iterate through all cookies and delete them such as this

    NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
       if (cookies != nil && cookies.count > 0) {
           for (NSHTTPCookie *cookie in cookies) {
               [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
           }
           [[NSUserDefaults standardUserDefaults] synchronize];
       }
    
    0 讨论(0)
  • 2020-12-17 03:44

    The similar for previous one but simplier:

    NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    
    NSArray *allCookies = [cookies cookies];
    
    for(NSHTTPCookie *cookie in allCookies) {
        if([[cookie domain] contains:@"facebook.com"]) {
            [cookies deleteCookie:cookie];
        }
    }
    

    The "best answer" is bad because it allows to delete cookies for the specified concrete URLs. So for example you delete cookie for "login.facebook.com" but you may miss "www.login.facebook.com"

    0 讨论(0)
  • 2020-12-17 03:48

    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];
        }
    }
    
    0 讨论(0)
提交回复
热议问题