I am new in iphone i am developing a application which is read cookies. but how do this i do not know plz help me.
Asked
Active
Viewed 7,827 times
1
-
Just a note.. You cant access cookies that is created by other Applications because of the Sandbox. – ipraba Apr 01 '11 at 18:43
-
possible duplicate of [Where are an UIWebView's cookies stored?](http://stackoverflow.com/questions/771498/where-are-an-uiwebviews-cookies-stored) – Nick Weaver May 20 '11 at 06:59
2 Answers
3
See this question on how to access cookies. You need to use the [NSHTTPCookieStorage sharedHTTPCookieStorage]
:
NSHTTPCookie *cookie;
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [cookieJar cookies]) {
NSLog(@"%@", cookie);
}
1
it's pretty easy. The code below should get you started. In my case, I just needed the value in 'Set-Cookie' but you can inspect what is in the headerFields dictionary and pull what you need. For more advanced cookies, you may need to pull a cookie and do some string parsing. All depends on what your service provides.
// get cookie data
NSDictionary *headerFields = [connection.response allHeaderFields];
NSString *customCookie = [headerFields valueForKey:@"Set-Cookie"];

Nathan Jones
- 1,836
- 14
- 13
-
When receiving headers with multiple "Set-Cookie" entries, the dictionary appears to be combined them (into a single dictionary item). Are there safe parsing rules to separate them again? – Robert Altman Jun 24 '11 at 16:05
-
I'm sure there are some parsing libraries out there or you write your own pretty quickly - I believe the format is always
= – Nathan Jones Jun 30 '11 at 02:56& = - but I haven't explored too closely. You could also check out NSHTTPCookieStorage for something that's more appropriate for your needs - http://developer.apple.com/library/ios/#documentation/cocoa/reference/foundation/Classes/NSHTTPCookieStorage_Class/Reference/Reference.html