deleted NSHTTPCookie returns if app is terminated - iphone

After using some code to delete all of the cookies:
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies]) {
[storage deleteCookie:cookie];
}
if you continue to use the app for a period of time, the cookies stay deleted. however, if you terminate the app immediately afterwards, the cookies will come back. sounds like some kind of cookie sync mechanism isn't kicking in fast enough, but the's no mention of it in the HTTPCookieStore docs.
How do you get a cookie to (reliably) stay deleted?

I do not think that there is a way to do that. Cookies are cached before they are sent to the NSHTTPCookie class and the same thing happens when you delete cookies. The class is told to delete the Cookies on quit, but won't if the app crashes as it doesn't catch the appropriate event.

Related

Cookie from ios webview

I wanted to ask some things about a piece of code i'm trying to make without having any previous contact with ios or objective-c.
This piece of code will:
Open a WebView with a specific url where the user will login
(done)
After the user logs in it will take the cookie created
from that login.
It will use that cookie in a next request to
load another site that requires authentication.
I'm stuck a bit at part 2 because it has to a) wait in another thread till the user does the login (how?) and b) because i can't seem to get the specific cookie for the site easily. I have only found and tried this poc but how do I filter out only the cookie for the site I want?
NSHTTPCookie *cookie;
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for(cookie in [cookieJar cookies]) {
NSLog(#"%#", cookie);
}
Any ideas on how to make the 2a/b parts? Objective-c syntax seems a bit confusing.
I found a solution by following this https://www.inkling.com/read/learning-ios-programming-alasdair-allan-2nd/chapter-7/embedding-a-web-browser-in-your .
Used the webViewDidFinishLoad delegate and got the cookies from there with the cookiesForURL method.
NSArray* availableCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:#"MYURL"]];
Cookies are created as follows,
NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
url, NSHTTPCookieOriginURL,
#"testCookies", NSHTTPCookieName,
#"1", NSHTTPCookieValue,
nil];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties];
You can use the cookie object to get the origin URL attribute and filter.
NSDictionary *cookieProperties = [cookie properties];
NSURL *originURL = [cookieProperties objectForKey:NSHTTPCookieOriginURL];
After filtering the cookie you need you can get the cookie value using,
cookie.value

logout from facebook completely

I have integrated facebook SDK and able to login to facebook. If user is enabled with facebook account on iOS settings then it ask for permission to acces in app otherwise it goes to safari for facebook login and come back to app. For logout I used following code:
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies])
{
NSString* domainName = [cookie domain];
NSRange domainRange = [domainName rangeOfString:#"facebook"];
if(domainRange.length > 0)
{
[storage deleteCookie:cookie];
}
}
[FBSession.activeSession closeAndClearTokenInformation];
But it is unable to logout completely. If I again click the login button it still showing access token as well as not moving to safari for facebook login again it means it has not logged out completely from facebook. I am unable to recognized the issue. If anyone know about this please help me out.
I would be very thankful to you.
Are you using version 3.x of the Facebook SDK for iOS? If so, the FBSession class should have a method called
-[FBSession closeAndClearTokenInformation]
Hopefully this will work for you.

How do I clear the browser cache programmatically on the iPhone?

I am using the new Basecamp API for my iOS basecamp client app. I want the user to be able to logout and switch accounts. But I can't as the account credentials stored in the browser cache are used every time I request authorization.
I figured out I would need to flush browser cache to do this. How do I clear the browser cache?
[[NSURLCache sharedURLCache] removeAllCachedResponses];
After that, you can deleting any associated cookies with in the UIWebView:
for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
if([[cookie domain] isEqualToString:someNSStringUrlDomain]) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
}

Do requests in UIWebView use sharedHTTPCookieStorage?

From googling a bit, the answer to my question is "yes". But how do I test that?
I tried to inpsect the HTTP request headers (i.e. [request allHTTPHeaderFields]) in webView:shouldStartLoadWithRequest:navigationType:, but none of the request has the "Cookie" entry, even though cookies are successfully stored in [NSHTTPCookieStorage sharedHTTPCookieStorage].
try this to loop through all the cookies in the UIWebView to verify they are there
NSHTTPCookie *aCookie;
for (aCookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
NSLog(#"%#", aCookie);
}

iphone NSHTTPCookieStorage avaible on app reopen?

I am working on my app here- and it pretty much comes to this. I have a login box where a users log's in and then it saves the cookie data on return like so:
NSArray * all = [NSHTTPCookie cookiesWithResponseHeaderFields:[resp allHeaderFields] forURL:[NSURL URLWithString:#"http://myurl]];
NSHTTPCookieStorage *sharedHTTPCookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
[sharedHTTPCookieStorage setCookies:all forURL:[NSURL URLWithString:#"http://myurl"] mainDocumentURL:nil];
After it safes that cookie i take it to the home view - My problem is - if the users closes the prgoram, the phone restarts and so forth - are the cookies stored locally on the phone its sefl? I am trying to access that cookie again on the didFinishLaunchingWithOptions. I have the following code now..
NSHTTPCookieStorage *sharedHTTPCookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *cookies = [sharedHTTPCookieStorage cookiesForURL:[NSURL URLWithString:#"http://iphone.wazgood.com"]];
NSLog(#"count: %i", [cookies count]);
Every time - it comes up empty on the cookie data - any ideas on if the cookies are cleared every time the user clsoes the program out - or is it bc im testing on the iPhone emulator?
In case you or anyone else is still having this problem, it could be that the cookies are set to expire when the session ends (when the app closes). You can check for this behavior by looking at the sessionOnly property of your NSHTTPCookies (the getter method is -(BOOL)isSessionOnly).