NSHTTPCookie NSHTTPCookieExpires
These constants define the supported keys in a dictionary containing cookie attributes.
extern NSString *NSHTTPCookieComment;
extern NSString *NSHTTPCookieCommentURL;
extern NSString *NSHTTPCookieDiscard;
extern NSString *NSHTTPCookieDomain;
extern NSString *NSHTTPCookieExpires;
extern NSString *NSHTTPCookieMaximumAge;
extern NSString *NSHTTPCookieName;
extern NSString *NSHTTPCookieOriginURL;
extern NSString *NSHTTPCookiePath;
extern NSString *NSHTTPCookiePort;
extern NSString *NSHTTPCookieSecure;
extern NSString *NSHTTPCookieValue;
extern NSString *NSHTTPCookieVersion;
Constants
NSHTTPCookieComment
An NSString object containing the comment for the cookie.
Only valid for Version 1 cookies and later. This header field is optional.
NSHTTPCookieCommentURL
An NSURL object or NSString object containing the comment URL for the cookie.
Only valid for Version 1 cookies or later. This header field is optional.
NSHTTPCookieDiscard
An NSString object stating whether the cookie should be discarded at the end of the session.
String value must be either “TRUE” or “FALSE”. This header field is optional. Default is “FALSE”, unless this is cookie is version 1 or greater and a value for NSHTTPCookieMaximumAge is not specified, in which case it is assumed “TRUE”.
NSHTTPCookieDomain
An NSString object containing the domain for the cookie.
A value must be specified for either NSHTTPCookieDomain or NSHTTPCookieOriginURL. If this header field is missing the domain is inferred from the value for NSHTTPCookieOriginURL.
NSHTTPCookieExpires
An NSDate object or NSString object specifying the expiration date for the cookie.
This header field is only used for Version 0 cookies. This header field is optional.
NSHTTPCookieMaximumAge
An NSString object containing an integer value stating how long in seconds the cookie should be kept, at most.
Only valid for Version 1 cookies and later. Default is “0”. This field is optional.
NSHTTPCookieName
An NSString object containing the name of the cookie. This field is required.
NSHTTPCookieOriginURL
An NSURL or NSString object containing the URL that set this cookie.
A value must be specified for either NSHTTPCookieDomain or NSHTTPCookieOriginURL.
NSHTTPCookiePath
An NSString object containing the path for the cookie. This field is required if you are using the NSHTTPCookieDomain key instead of the NSHTTPCookieOriginURL key.
If you are using the NSHTTPCookieOriginURL key, the path is inferred if it is not provided. The default value is “/”.
NSHTTPCookiePort
An NSString object containing comma-separated integer values specifying the ports for the cookie.
Only valid for Version 1 cookies or later. The default value is an empty string (““). This header field is optional.
NSHTTPCookieSecure
An NSString object indicating that the cookie should be transmitted only over secure channels.
Providing any value for this key indicates that the cookie should remain secure.
NSHTTPCookieValue
An NSString object containing the value of the cookie.
This header field is required.
NSHTTPCookieVersion
An NSString object that specifies the version of the cookie.
Must be either “0” or “1”. The default is “0”. This header field is optional.
NSHTTPCookie NSHTTPCookieExpires example.
[[NSMutableArray alloc] init];
for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
[cookieArray addObject:cookie.name];
NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
[cookieProperties setObject:cookie.name forKey:NSHTTPCookieName];
[cookieProperties setObject:cookie.value forKey:NSHTTPCookieValue];
[cookieProperties setObject:cookie.domain forKey:NSHTTPCookieDomain];
[cookieProperties setObject:cookie.path forKey:NSHTTPCookiePath];
[cookieProperties setObject:[NSNumber numberWithInt:cookie.version] forKey:NSHTTPCookieVersion];
[cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires];
[[NSUserDefaults standardUserDefaults] setValue:cookieProperties forKey:cookie.name];
[[NSUserDefaults standardUserDefaults] synchronize];
}
[[NSUserDefaults standardUserDefaults] setValue:cookieArray forKey:@"cookieArray"];
[[NSUserDefaults standardUserDefaults] synchronize];
for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
[cookieArray addObject:cookie.name];
NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
[cookieProperties setObject:cookie.name forKey:NSHTTPCookieName];
[cookieProperties setObject:cookie.value forKey:NSHTTPCookieValue];
[cookieProperties setObject:cookie.domain forKey:NSHTTPCookieDomain];
[cookieProperties setObject:cookie.path forKey:NSHTTPCookiePath];
[cookieProperties setObject:[NSNumber numberWithInt:cookie.version] forKey:NSHTTPCookieVersion];
[cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires];
[[NSUserDefaults standardUserDefaults] setValue:cookieProperties forKey:cookie.name];
[[NSUserDefaults standardUserDefaults] synchronize];
}
[[NSUserDefaults standardUserDefaults] setValue:cookieArray forKey:@"cookieArray"];
[[NSUserDefaults standardUserDefaults] synchronize];
Example of [NSHTTPCookie NSHTTPCookieExpires].
NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
[cookieProperties setObject:@"testCookie" forKey:NSHTTPCookieName];
[cookieProperties setObject:@"someValue123456" forKey:NSHTTPCookieValue];
[cookieProperties setObject:@"www.example.com" forKey:NSHTTPCookieDomain];
[cookieProperties setObject:@"www.example.com" forKey:NSHTTPCookieOriginURL];
[cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];
[cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion];
// 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];
[cookieProperties setObject:@"testCookie" forKey:NSHTTPCookieName];
[cookieProperties setObject:@"someValue123456" forKey:NSHTTPCookieValue];
[cookieProperties setObject:@"www.example.com" forKey:NSHTTPCookieDomain];
[cookieProperties setObject:@"www.example.com" forKey:NSHTTPCookieOriginURL];
[cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];
[cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion];
// 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];
NSHTTPCookie NSHTTPCookieExpires example.
NSArray *newCookie = [NSArray arrayWithArray:[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]];
for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
for (NSHTTPCookie *cookie in newCookie) {
NSMutableDictionary *properties = [NSMutableDictionary dictionaryWithDictionary:cookie.properties];
[properties removeObjectForKey:NSHTTPCookieExpires];
NSHTTPCookie *changeCookie = [NSHTTPCookie cookieWithProperties:properties];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:changeCookie];
for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
for (NSHTTPCookie *cookie in newCookie) {
NSMutableDictionary *properties = [NSMutableDictionary dictionaryWithDictionary:cookie.properties];
[properties removeObjectForKey:NSHTTPCookieExpires];
NSHTTPCookie *changeCookie = [NSHTTPCookie cookieWithProperties:properties];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:changeCookie];