不删除cookie(iphone)

时间:2011-07-21 15:52:39

标签: iphone objective-c xcode nshttpcookie

        NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];

        NSArray *cookies = [storage cookiesForURL:[NSURL URLWithString:@"http://www.facebook.com/"]];
        NSLog(@"old cookies!: %@",cookies);
        NSHTTPCookie *cookie;
        for (cookie in [storage cookies]) {
            [storage deleteCookie:cookie];
        }
        NSLog(@"new cookies!: %@",cookies);

我正在尝试在注销功能中删除facebook cookie,但没有删除cookie。我不懂为什么。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

如果您希望保留对NSHTTPCookieStorage的更改,您还需要在修改cookie存储后同步standardUserDefaults:

[[NSUserDefaults standardUserDefaults] synchronize];

为了防止这种情况减慢您的应用,您可能还想在后台线程上调用它:

dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);

dispatch_async(backgroundQueue, ^{
    //TODO: Cookie deletion logic here
});

编辑:

如果你只需要为给定的NSURLRequest完全忽略cookie,你可以这样做:

[request setHTTPShouldHandleCookies:NO];

请求是您的NSURLRequest实例。