Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Destroy NSHTTPCookies?

I am running a Post call to a URL and logging into a system. This is working, but after the login, I am attempting to retrieve the cookies – but when I do this:

NSArray *allCookies = [NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:[NSURL URLWithString:mywebsite"]]; 
NSLog(@"How many Cookies: %d", allCookies.count);

I am getting a response of “How many Cookies: 0”

The problem is that when I close the App and then reopen, it is connecting using the info from my previous login.

What is going on here? Why is it telling me there are zero cookies? How can I get rid of these cookies?

like image 259
Chris Avatar asked Jan 29 '26 17:01

Chris


1 Answers

See if you can find and delete them this way:

NSHTTPCookieStorage * sharedCookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray * cookies = [sharedCookieStorage cookies];
for (NSHTTPCookie * cookie in cookies){
    NSLog(@"%@",cookie.domain);
    if ([cookie.domain rangeOfString:@"yourdomain"].location != NSNotFound){
            NSLog(@"deleting");
            [sharedCookieStorage deleteCookie:cookie];
    }
}
like image 116
Tom Irving Avatar answered Feb 01 '26 09:02

Tom Irving



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!