I'm logging into an API via [NSHTTPCookieStorage setCookie]
. http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSHTTPCookieStorage_Class/Reference/Reference.html
If I log in while my browser is open, already logged in with the same user to the same site, both the app and the browser continue to work fine.
But if I Quit
my browser, open it up again, and go back to the site, the cookie has been lost. User is not logged in.
What's going on here, and is there a way to force the app to use a different cookie than the browser?
Alternately, if we are "sharing" cookies between apps, is there any way I can use the already-logged-in user's cookie to log into my app without asking for a username/password?
Here's the code for create a new cookie, as you requested: (Not tested in XCode)
// Dictionary of attributes for the new cookie
NSDictionary *newCookieDict = [NSMutableDictionary
dictionaryWithObjectsAndKeys:@".example.com", NSHTTPCookieDomain,
@"Test Cookie", NSHTTPCookieName,
@"/", NSHTTPCookiePath,
@"test1234567890", NSHTTPCookieValue,
@“2011-10-26 00:00:00 -0700", NSHTTPCookieExpires, nil];
// Create a new cookie
NSHTTPCookie *newCookie = [NSHTTPCookie cookieWithProperties:newCookieDict];
// Add the new cookie
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:newCookie];
Special attention to the last element of the dictionary
Have you set an expiration date for the cookie? If you don't, NSHTTPCookie assumes to be session only ( link to docs )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.