Sunday, June 16, 2013

NSHTTPCookieStorage deleteCookie example in Objective C (iOS).


NSHTTPCookieStorage deleteCookie

Deletes the specified cookie from the cookie storage.

- (void)deleteCookie:(NSHTTPCookie *)aCookie

Parameters of [NSHTTPCookieStorage deleteCookie]
aCookie
The cookie to delete.

NSHTTPCookieStorage deleteCookie example.
NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
        NSArray* facebookCookies = [cookies cookiesForURL:
                                    [NSURL URLWithString:@"http://login.facebook.com"]];
        for (NSHTTPCookie* cookie in facebookCookies) {
            [cookies deleteCookie:cookie];
        }

Example of [NSHTTPCookieStorage deleteCookie].
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (NSHTTPCookie *each in cookieStorage.cookies) {
        [cookieStorage deleteCookie:each];
    }

NSHTTPCookieStorage deleteCookie example.
NSArray *newCookie = [NSArray arrayWithArray:[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]];

for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
for (NSHTTPCookie *cookie in newCookie) {
    NSMutableDictionary *properties = [NSMutableDictionary dictionaryWithDictionary:cookie.properties];
    [properties removeObjectForKey:NSHTTPCookieExpires];
     NSHTTPCookie *changeCookie = [NSHTTPCookie cookieWithProperties:properties];
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:changeCookie];

End of NSHTTPCookieStorage deleteCookie example article.