Constants
NSHTTPCookieAcceptPolicyAlways
Accept all cookies. This is the default cookie accept policy.
NSHTTPCookieAcceptPolicyNever
Reject all cookies.
NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain
Accept cookies only from the main document domain.
Parameters of [NSHTTPCookieStorage setCookies]
cookies
The cookies to add.
theURL
The URL associated with the added cookies.
mainDocumentURL
The URL of the main HTML document for the top-level frame, if known. Can be nil. This URL is used to determine if the cookie should be accepted if the cookie accept policy is NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain.
Discussion of [NSHTTPCookieStorage setCookies]
The cookies will replace existing cookies with the same name, domain, and path, if one exists in the cookie storage. The cookie will be ignored if the receiver's cookie accept policy is NSHTTPCookieAcceptPolicyNever.
To store cookies from a set of response headers, an application can use cookiesWithResponseHeaderFields:forURL: passing a header field dictionary and then use this method to store the resulting cookies in accordance with the receiver’s cookie acceptance policy.
// If you want to get all of the cookies:
NSArray * all = [NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:[NSURL URLWithString:@"http://temp"]];
NSLog(@"How many Cookies: %d", all.count);
// Store the cookies:
// NSHTTPCookieStorage is a Singleton.
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:all forURL:[NSURL URLWithString:@"http://temp"] mainDocumentURL:nil];
// Now we can print all of the cookies we have:
for (NSHTTPCookie *cookie in all)
NSLog(@"Name: %@ : Value: %@, Expires: %@", cookie.name, cookie.value, cookie.expiresDate);
// Now lets go back the other way. We want the server to know we have some cookies available:
// this availableCookies array is going to be the same as the 'all' array above. We could
// have just used the 'all' array, but this shows you how to get the cookies back from the singleton.
NSArray * availableCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:@"http://temp"]];
NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:availableCookies];
// we are just recycling the original request
[request setAllHTTPHeaderFields:headers];
Parameters of [NSHTTPCookieStorage setCookieAcceptPolicy]
aPolicy
The new cookie accept policy.
Discussion of [NSHTTPCookieStorage setCookieAcceptPolicy]
The default cookie accept policy is NSHTTPCookieAcceptPolicyAlways. Changing the cookie policy affects all currently running applications using the cookie storage.
First, in applicationDidBecomeActive add this line
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
The cookieAcceptPolicy is shared across apps and can be changed without your knowledge, so you want to be sure you have the accept policy you need every time your app is running.
// set expiration to one month from now or any NSDate of your choosing
// this makes the cookie sessionless and it will persist across web sessions and app launches
/// if you want the cookie to be destroyed when your app exits, don't set this
[cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
This cookie has the name testCookie and value someValue123456 and will be sent with any http request to www.example.com.
Example of [NSHTTPCookieStorage setCookieAcceptPolicy].
Try changing the cookie acceptance policy instead:
Stores a specified cookie in the cookie storage if the cookie accept policy permits.
- (void)setCookie:(NSHTTPCookie *)aCookie
Parameters of [NSHTTPCookieStorage setCookie]
aCookie
The cookie to store.
Discussion of [NSHTTPCookieStorage setCookie]
The cookie replaces an existing cookie with the same name, domain, and path, if one exists in the cookie storage. This method accepts the cookie only if the receiver’s cookie accept policy is NSHTTPCookieAcceptPolicyAlways or NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain. The cookie is ignored if the receiver’s cookie accept policy is NSHTTPCookieAcceptPolicyNever.
NSHTTPCookieStorage setCookie example.
// 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];
Example of [NSHTTPCookieStorage setCookie].
First, in applicationDidBecomeActive add this line
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
The cookieAcceptPolicy is shared across apps and can be changed without your knowledge, so you want to be sure you have the accept policy you need every time your app is running.
// set expiration to one month from now or any NSDate of your choosing
// this makes the cookie sessionless and it will persist across web sessions and app launches
/// if you want the cookie to be destroyed when your app exits, don't set this
[cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
This cookie has the name testCookie and value someValue123456 and will be sent with any http request to www.example.com.
NSHTTPCookieStorage setCookie example.
1) Save cookie when they get created by uiwebview.
[[NSUserDefaults standardUserDefaults] setValue:cookieArray forKey:@"cookieArray"];
[[NSUserDefaults standardUserDefaults] synchronize];
2) Now recreate them as app restarts:
Returns all the cookie storage’s cookies that are sent to a specified URL.
- (NSArray *)cookiesForURL:(NSURL *)theURL
Parameters of [NSHTTPCookieStorage cookiesForURL]
theURL
The URL to filter on.
Return Value
An array of cookies whose URL matches the provided URL.
Discussion of [NSHTTPCookieStorage cookiesForURL]
An application can use NSHTTPCookie method requestHeaderFieldsWithCookies: to turn this array into a set of header fields to add to an NSMutableURLRequest object.
NSHTTPCookieStorage cookiesForURL example.
first check if
[[NSHTTPCookieStorage sharedHTTPCookieStorage]
cookiesForURL:[NSURL URLWithString:theCookie]]
returns what you think it should return. If not, take a closer look at the value of theCookie.
cookies = [[ NSHTTPCookieStorage sharedHTTPCookieStorage ]
cookiesForURL: self.url ];
if ( !cookies ) {
/* kick off new NSURLConnection to retrieve new auth cookie */
return;
}
// If you want to get all of the cookies:
NSArray * all = [NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:[NSURL URLWithString:@"http://temp"]];
NSLog(@"How many Cookies: %d", all.count);
// Store the cookies:
// NSHTTPCookieStorage is a Singleton.
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:all forURL:[NSURL URLWithString:@"http://temp"] mainDocumentURL:nil];
// Now we can print all of the cookies we have:
for (NSHTTPCookie *cookie in all)
NSLog(@"Name: %@ : Value: %@, Expires: %@", cookie.name, cookie.value, cookie.expiresDate);
// Now lets go back the other way. We want the server to know we have some cookies available:
// this availableCookies array is going to be the same as the 'all' array above. We could
// have just used the 'all' array, but this shows you how to get the cookies back from the singleton.
NSArray * availableCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:@"http://temp"]];
NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:availableCookies];
// we are just recycling the original request
[request setAllHTTPHeaderFields:headers];
Return Value
An array containing all of the cookie storage’s cookies.
Availability
Available in iOS 2.0 and later.
Discussion of [NSHTTPCookieStorage cookies]
If you want to sort the cookie storage’s cookies, you should use the sortedCookiesUsingDescriptors: method instead of sorting the result of this method.
NSHTTPCookieStorage cookies example.
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *each in cookieStorage.cookies) {
[cookieStorage deleteCookie:each];
}