I can create a cookie, but can't delete it from my iPhone app

Posted by squeezemylime on Stack Overflow See other posts from Stack Overflow or by squeezemylime
Published on 2010-06-07T22:09:31Z Indexed on 2010/06/07 22:12 UTC
Read the original article Hit count: 318

Filed under:
|
|
|

I am creating an iPhone app, and am using this method to create a cookie that will be accessed site-wide:

NSMutableDictionary *cookieDictionary = [NSMutableDictionary dictionaryWithCapacity:4];

    [cookieDictionary setObject:@"status" forKey:NSHTTPCookieName];
    [cookieDictionary setObject:[self.usernameField text] forKey:NSHTTPCookieValue];
    [cookieDictionary setObject:@"http://www.mydomain.com" forKey:NSHTTPCookieDomain];
    [cookieDictionary setObject:@"/" forKey:NSHTTPCookiePath];

    // Build the cookie.
    NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieDictionary];

    // Store the cookie.
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];

    // Log the Cookie and display it
    for (cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
        NSLog(@"%@", cookie.value);
    }

Now I am trying to delete it via the following method, but it isn't working, and the documentation isn't quite helping me:

NSHTTPCookieStorage* cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray* theCookies = [cookieStorage cookiesForURL:[NSURL URLWithString:@"http://mydomain.com"]];

for (NSHTTPCookie *cookie in theCookies) {
    [cookieStorage deleteCookie:cookie];
}

© Stack Overflow or respective owner

Related posts about iphone

Related posts about cookies