Showing posts with label NSHTTPCookie example. Show all posts
Showing posts with label NSHTTPCookie example. Show all posts

Wednesday, June 12, 2013

NSHTTPCookie NSHTTPCookieVersion example in Objective C (iOS).


NSHTTPCookie NSHTTPCookieVersion

HTTP Cookie Property Keys
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 NSHTTPCookieVersion example.
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];

NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
[cookieProperties setObject:@"mobileApp" forKey:NSHTTPCookieName];
[cookieProperties setObject:@"1" 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];

NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}

Example of [NSHTTPCookie NSHTTPCookieVersion].
- (void)applicationDidBecomeActive:(UIApplication *)application
{

    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];

    NSHTTPCookie *cookie;
    for (cookie in [NSHTTPCookieStorage sharedHTTPCookieStorage].cookies) {
        NSLog(@"%@=%@", cookie.name, cookie.value);
    }

    NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
    [cookieProperties setObject:@"testCookie" forKey:NSHTTPCookieName];
    [cookieProperties setObject:[NSString stringWithFormat:@"%f", [[NSDate date] timeIntervalSince1970]] 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
    [cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires];

    cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];

}

NSHTTPCookie NSHTTPCookieVersion example.
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];

End of NSHTTPCookie NSHTTPCookieVersion example article.

NSHTTPCookie NSHTTPCookieValue example in Objective C (iOS).

NSHTTPCookie NSHTTPCookieValue

HTTP Cookie Property Keys
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 NSHTTPCookieValue example.
NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"domain.com", NSHTTPCookieDomain,
                            @"\\", NSHTTPCookiePath,  // IMPORTANT!
                            @"testCookies", NSHTTPCookieName,
                            @"1", NSHTTPCookieValue,
                            nil];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties];

NSArray* cookies = [NSArray arrayWithObjects: cookie, nil];

NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];

[request setAllHTTPHeaderFields:headers];

Example of [NSHTTPCookie NSHTTPCookieValue].
NSDictionary *cookieDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"SID",NSHTTPCookieName,
          self.sessionID,NSHTTPCookieValue,
          @".google.com",NSHTTPCookieDomain,
          @"/",NSHTTPCookiePath,
          nil];

NSHTTPCookie NSHTTPCookieValue example.
NSURL *serverURL = [NSURL URLWithString:@"<Server URL>"];

NSHTTPCookie *usernamecookie = [NSHTTPCookie cookieWithProperties:
                                   [NSDictionary dictionaryWithObjectsAndKeys:
                                   [serverURL host], NSHTTPCookieDomain,
                                   [serverURL path], NSHTTPCookiePath,
                                     @"username", NSHTTPCookieName,
                                     @"<username>", NSHTTPCookieValue,
                                     nil]];

NSHTTPCookie *passwordcookie = [NSHTTPCookie cookieWithProperties:
                                   [NSDictionary dictionaryWithObjectsAndKeys:
                                   [serverURL host], NSHTTPCookieDomain,
                                   [serverURL path], NSHTTPCookiePath,
                                     @"password", NSHTTPCookieName,
                                     @"<password>", NSHTTPCookieValue,
                                      nil]];

[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:usernamecookie];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:passwordcookie];

NSData *responseData = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:serverURL] returningResponse:nil error:nil];
NSString *response = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]autorelease];
NSLog(@"response data %@",[response description]);

End of NSHTTPCookie NSHTTPCookieValue example article.

NSHTTPCookie NSHTTPCookiePath example in Objective C (iOS).

NSHTTPCookie NSHTTPCookiePath

HTTP Cookie Property Keys
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 NSHTTPCookiePath example.
NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"domain.com", NSHTTPCookieDomain,
                            @"\\", NSHTTPCookiePath,  // IMPORTANT!
                            @"testCookies", NSHTTPCookieName,
                            @"1", NSHTTPCookieValue,
                            nil];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties];

NSArray* cookies = [NSArray arrayWithObjects: cookie, nil];

NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];

[request setAllHTTPHeaderFields:headers];

Example of [NSHTTPCookie NSHTTPCookiePath].
NSDictionary *cookieDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"SID",NSHTTPCookieName,
          self.sessionID,NSHTTPCookieValue,
          @".google.com",NSHTTPCookieDomain,
          @"/",NSHTTPCookiePath,
          nil];

NSHTTPCookie NSHTTPCookiePath example.
NSURL *serverURL = [NSURL URLWithString:@"<Server URL>"];

NSHTTPCookie *usernamecookie = [NSHTTPCookie cookieWithProperties:
                                   [NSDictionary dictionaryWithObjectsAndKeys:
                                   [serverURL host], NSHTTPCookieDomain,
                                   [serverURL path], NSHTTPCookiePath,
                                     @"username", NSHTTPCookieName,
                                     @"<username>", NSHTTPCookieValue,
                                     nil]];

NSHTTPCookie *passwordcookie = [NSHTTPCookie cookieWithProperties:
                                   [NSDictionary dictionaryWithObjectsAndKeys:
                                   [serverURL host], NSHTTPCookieDomain,
                                   [serverURL path], NSHTTPCookiePath,
                                     @"password", NSHTTPCookieName,
                                     @"<password>", NSHTTPCookieValue,
                                      nil]];

[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:usernamecookie];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:passwordcookie];

NSData *responseData = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:serverURL] returningResponse:nil error:nil];
NSString *response = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]autorelease];
NSLog(@"response data %@",[response description]);

End of NSHTTPCookie NSHTTPCookiePath example article.

NSHTTPCookie NSHTTPCookieOriginURL example in Objective C (iOS).

NSHTTPCookie NSHTTPCookieOriginURL

HTTP Cookie Property Keys
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 NSHTTPCookieOriginURL example.
NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
                              url, NSHTTPCookieOriginURL,
                              @"testCookies", NSHTTPCookieName,
                              @"1", NSHTTPCookieValue,
                              nil];
  NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties];

Example of [NSHTTPCookie NSHTTPCookieOriginURL].
NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
[cookieProperties setObject:@"mobileApp" forKey:NSHTTPCookieName];
[cookieProperties setObject:@"1" 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];

NSHTTPCookie NSHTTPCookieOriginURL example.
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];

End of NSHTTPCookie NSHTTPCookieOriginURL example article.

NSHTTPCookie NSHTTPCookieName example in Objective C (iOS).

NSHTTPCookie NSHTTPCookieName

HTTP Cookie Property Keys
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 NSHTTPCookieName example.
NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
                              url, NSHTTPCookieOriginURL,
                              @"testCookies", NSHTTPCookieName,
                              @"1", NSHTTPCookieValue,
                              nil];
  NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties];

Example of [NSHTTPCookie NSHTTPCookieName].
NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"domain.com", NSHTTPCookieDomain,
                            @"\\", NSHTTPCookiePath,  // IMPORTANT!
                            @"testCookies", NSHTTPCookieName,
                            @"1", NSHTTPCookieValue,
                            nil];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties];

NSArray* cookies = [NSArray arrayWithObjects: cookie, nil];

NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];

[request setAllHTTPHeaderFields:headers];

NSHTTPCookie NSHTTPCookieName example.
NSURL *serverURL = [NSURL URLWithString:@"<Server URL>"];

NSHTTPCookie *usernamecookie = [NSHTTPCookie cookieWithProperties:
                                   [NSDictionary dictionaryWithObjectsAndKeys:
                                   [serverURL host], NSHTTPCookieDomain,
                                   [serverURL path], NSHTTPCookiePath,
                                     @"username", NSHTTPCookieName,
                                     @"<username>", NSHTTPCookieValue,
                                     nil]];

NSHTTPCookie *passwordcookie = [NSHTTPCookie cookieWithProperties:
                                   [NSDictionary dictionaryWithObjectsAndKeys:
                                   [serverURL host], NSHTTPCookieDomain,
                                   [serverURL path], NSHTTPCookiePath,
                                     @"password", NSHTTPCookieName,
                                     @"<password>", NSHTTPCookieValue,
                                      nil]];

[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:usernamecookie];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:passwordcookie];

NSData *responseData = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:serverURL] returningResponse:nil error:nil];
NSString *response = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]autorelease];
NSLog(@"response data %@",[response description]);

End of NSHTTPCookie NSHTTPCookieName example article.

NSHTTPCookie NSHTTPCookieExpires example in Objective C (iOS).

NSHTTPCookie NSHTTPCookieExpires

HTTP Cookie Property Keys
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];

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];

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];

End of NSHTTPCookie NSHTTPCookieExpires example article.

NSHTTPCookie NSHTTPCookieDomain example in Objective C (iOS).

NSHTTPCookie NSHTTPCookieDomain

HTTP Cookie Property Keys
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 NSHTTPCookieDomain example.
NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"domain.com", NSHTTPCookieDomain,
                            @"\\", NSHTTPCookiePath,  // IMPORTANT!
                            @"testCookies", NSHTTPCookieName,
                            @"1", NSHTTPCookieValue,
                            nil];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties];

NSArray* cookies = [NSArray arrayWithObjects: cookie, nil];

NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];

[request setAllHTTPHeaderFields:headers];

Example of [NSHTTPCookie NSHTTPCookieDomain].
NSURL *serverURL = [NSURL URLWithString:@"<Server URL>"];

NSHTTPCookie *usernamecookie = [NSHTTPCookie cookieWithProperties:
                                   [NSDictionary dictionaryWithObjectsAndKeys:
                                   [serverURL host], NSHTTPCookieDomain,
                                   [serverURL path], NSHTTPCookiePath,
                                     @"username", NSHTTPCookieName,
                                     @"<username>", NSHTTPCookieValue,
                                     nil]];

NSHTTPCookie *passwordcookie = [NSHTTPCookie cookieWithProperties:
                                   [NSDictionary dictionaryWithObjectsAndKeys:
                                   [serverURL host], NSHTTPCookieDomain,
                                   [serverURL path], NSHTTPCookiePath,
                                     @"password", NSHTTPCookieName,
                                     @"<password>", NSHTTPCookieValue,
                                      nil]];

[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:usernamecookie];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:passwordcookie];

NSData *responseData = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:serverURL] returningResponse:nil error:nil];
NSString *response = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]autorelease];
NSLog(@"response data %@",[response description]);

NSHTTPCookie NSHTTPCookieDomain example.
NSDictionary *cookieDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"SID",NSHTTPCookieName,
          self.sessionID,NSHTTPCookieValue,
          @".google.com",NSHTTPCookieDomain,
          @"/",NSHTTPCookiePath,
          nil];

End of NSHTTPCookie NSHTTPCookieDomain example article.

NSHTTPCookie initWithProperties example in Objective C (iOS).

NSHTTPCookie initWithProperties

Returns an initialized NSHTTPCookie object using the provided properties.

- (id)initWithProperties:(NSDictionary *)properties

Parameters of [NSHTTPCookie initWithProperties]
properties
The properties for the new cookie object, expressed as key value pairs.

Return Value
The initialized cookie object. Returns nil if the provided properties are invalid.

Discussion of [NSHTTPCookie initWithProperties]
See “Constants” for more information on the available header field constants and the constraints imposed on the values in the dictionary.

NSHTTPCookie initWithProperties example.
- (BOOL)readsSessionCookieFromKeyChain {
    NSError *readError = nil;
    NSString *jsonString = [SFHFKeychainUtils getPasswordForUsername:WSC_username
                                                      andServiceName:WSC_serviceName
                                                               error:&readError];
    if (!jsonString) return NO;
    NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; // be sure that data is in UTF8 or it won't work
    JSONDecoder* decoder = [[JSONDecoder alloc] initWithParseOptions:JKParseOptionNone];
    NSDictionary* jsonDict = (NSDictionary*)[decoder objectWithData:jsonData];
    NSLog(@"jsonDict: %@",jsonDict);

    NSHTTPCookie *cookie = [[NSHTTPCookie alloc] initWithProperties:jsonDict];
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];

    return cookie!=nil;
}

Example of [NSHTTPCookie initWithProperties].
NSDictionary *properties = [[[NSMutableDictionary alloc] init] autorelease];
[properties setValue:@"linpan" forKey:@"username"];
[properties setValue:@"123456" forKey:@"password"];
[properties setValue:@"JSESSIONID" forKey:jsessionId];
NSHTTPCookie *cookie2 = [[NSHTTPCookie alloc] initWithProperties:properties];
[indexRequest3 setRequestCookies:[NSMutableArray arrayWithObjects:cookie2,nil]];
[indexRequest3 setDelegate:self];
[indexRequest3 startAsynchronous];

NSHTTPCookie initWithProperties example.
//Createacookie
NSDictionary*properties=[[[NSMutableDictionaryalloc]init]autorelease];
[propertiessetValue:[@"TestValue"encodedCookieValue]forKey:NSHTTPCookieValue];
[propertiessetValue:@"ASIHTTPRequestTestCookie"forKey:NSHTTPCookieName];
[propertiessetValue:@".allseeing-i.com"forKey:NSHTTPCookieDomain];
[propertiessetValue:[NSDatedateWithTimeIntervalSinceNow:60*60]forKey:NSHTTPCookieExpires];
[propertiessetValue:@"/asi-http-request/tests"forKey:NSHTTPCookiePath];
NSHTTPCookie*cookie=[[[NSHTTPCookiealloc]initWithProperties:properties]autorelease];

//Thisurlwillreturnthevalueofthe'ASIHTTPRequestTestCookie'cookie
url=[NSURLURLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/read_cookie"];
request=[ASIHTTPRequestrequestWithURL:url];
[requestsetUseCookiePersistence:NO];
[requestsetRequestCookies:[NSMutableArrayarrayWithObject:cookie]];
[requeststartSynchronous];

End of NSHTTPCookie initWithProperties example article.

NSHTTPCookie expiresDate example in Objective C (iOS).

NSHTTPCookie expiresDate

Returns the receiver’s expiration date.

- (NSDate *)expiresDate

Return Value of [NSHTTPCookie expiresDate]
The receiver’s expiration date, or nil if there is no specific expiration date such as in the case of “session-only” cookies. The expiration date is the date when the cookie should be deleted.

NSHTTPCookie expiresDate example.
    NSHTTPURLResponse   * response;
    NSError             * error;
    NSMutableURLRequest * request;
    request = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://temp/gomh/authenticate.py?setCookie=1"]
                                            cachePolicy:NSURLRequestReloadIgnoringCacheData
                                        timeoutInterval:60] autorelease];

    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
    NSLog(@"RESPONSE HEADERS: \n%@", [response allHeaderFields]);

    // 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];

    request.URL = [NSURL URLWithString:@"http://temp/gomh/authenticate.py"];
    error       = nil;
    response    = nil;

    NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSLog(@"The server saw:\n%@", [[[NSString alloc] initWithData:data encoding: NSASCIIStringEncoding] autorelease]);

Example of [NSHTTPCookie expiresDate].
    for (NSHTTPCookie *cookie in all) {
        NSLog(@"Name: %@ : Value: %@", cookie.name, cookie.value);
        NSLog(@"Comment: %@ : CommentURL: %@", cookie.comment, cookie.commentURL);
        NSLog(@"Domain: %@ : ExpiresDate: %@", cookie.domain, cookie.expiresDate);
        NSLog(@"isHTTPOnly: %c : isSecure: %c", cookie.isHTTPOnly, cookie.isSecure);
        NSLog(@"isSessionOnly: %c : path: %@", cookie.isSessionOnly, cookie.path);
        NSLog(@"portList: %@ : properties: %@", cookie.portList, cookie.properties);
        NSLog(@"version: %u", cookie.version);
    }

NSHTTPCookie expiresDate example.
if ([[[object url] host] rangeOfString:domain].location != NSNotFound)
{
    NSHTTPCookie*cookie = [DAHTTPCookie createCookieWithURL:[ck domain] cookieName:[ck name] expires:[[ck expiresDate] timeIntervalSince1970] cookieValue:[ck value] browserType:DARavenBrowser secure:[ck isSecure]];
    [_output addObject:cookie];
}

End of NSHTTPCookie expiresDate example article.

NSHTTPCookie requestHeaderFieldsWithCookies example in Objective C (iOS).

NSHTTPCookie requestHeaderFieldsWithCookies

Returns a dictionary of header fields corresponding to a provided array of cookies.

+ (NSDictionary *)requestHeaderFieldsWithCookies:(NSArray *)cookies

Parameters of [NSHTTPCookie requestHeaderFieldsWithCookies]
cookies
The cookies from which the header fields are created.

Return Value
The dictionary of header fields created from the provided cookies. This dictionary can be used to add cookies to a request.

Discussion of [NSHTTPCookie requestHeaderFieldsWithCookies]
See “Constants” for details on the header field keys and values in the returned dictionary.

NSHTTPCookie requestHeaderFieldsWithCookies example.
requestHeaderFieldsWithCookies

Example of [NSHTTPCookie requestHeaderFieldsWithCookies].
    NSHTTPURLResponse   * response;
    NSError             * error;
    NSMutableURLRequest * request;
    request = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://temp/gomh/authenticate.py?setCookie=1"]
                                            cachePolicy:NSURLRequestReloadIgnoringCacheData
                                        timeoutInterval:60] autorelease];

    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
    NSLog(@"RESPONSE HEADERS: \n%@", [response allHeaderFields]);

    // 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];

    request.URL = [NSURL URLWithString:@"http://temp/gomh/authenticate.py"];
    error       = nil;

NSHTTPCookie requestHeaderFieldsWithCookies example.
- ( void )reloadWebview: (id)sender
{
    NSArray                 *cookies;
    NSDictionary            *cookieHeaders;
    NSMutableURLRequest     *request;

    cookies = [[ NSHTTPCookieStorage sharedHTTPCookieStorage ]
                cookiesForURL: self.url ];
    if ( !cookies ) {
        /* kick off new NSURLConnection to retrieve new auth cookie */
        return;
    }

    cookieHeaders = [ NSHTTPCookie requestHeaderFieldsWithCookies: cookies ];
    request = [[ NSMutableURLRequest alloc ] initWithURL: self.url ];
    [ request setValue: [ cookieHeaders objectForKey: @"Cookie" ]
              forHTTPHeaderField: @"Cookie" ];

    [ self.webView loadRequest: request ];
    [ request release ];
}

End of NSHTTPCookie requestHeaderFieldsWithCookies example article.

NSHTTPCookie cookieWithProperties example in Objective C (iOS).

NSHTTPCookie cookieWithProperties

Creates and initializes an NSHTTPCookie object using the provided properties.

+ (id)cookieWithProperties:(NSDictionary *)properties

Parameters of [NSHTTPCookie cookieWithProperties]
properties
The properties for the new cookie object, expressed as key value pairs.

Return Value
The newly created cookie object. Returns nil if the provided properties are invalid.

Discussion of [NSHTTPCookie cookieWithProperties]
See “Constants” for more information on the available header field constants and the constraints imposed on the values in the dictionary.

NSHTTPCookie cookieWithProperties example.
This is how you set properties in a cookie:

 NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
                              url, NSHTTPCookieOriginURL,
                              @"testCookies", NSHTTPCookieName,
                              @"1", NSHTTPCookieValue,
                              nil];
  NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties];

Example of [NSHTTPCookie cookieWithProperties].
NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"domain.com", NSHTTPCookieDomain,
                            @"\\", NSHTTPCookiePath,  // IMPORTANT!
                            @"testCookies", NSHTTPCookieName,
                            @"1", NSHTTPCookieValue,
                            nil];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties];

NSArray* cookies = [NSArray arrayWithObjects: cookie, nil];

NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];

[request setAllHTTPHeaderFields:headers];

NSHTTPCookie cookieWithProperties example.
// add cookie
    NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
                                req.URL.host, NSHTTPCookieDomain,
                                req.URL.path, NSHTTPCookiePath,
                                @"MLSTORAGE", NSHTTPCookieName,
                                @"1234567890", NSHTTPCookieValue,
                                nil];
    NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties];
    NSLog(@"\nurl: %@\ncookie: %@", req.URL, cookie);
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
    //

End of NSHTTPCookie cookieWithProperties example article.

NSHTTPCookie cookiesWithResponseHeaderFields example in Objective C (iOS).

NSHTTPCookie cookiesWithResponseHeaderFields

Returns an array of NSHTTPCookie objects corresponding to the provided response header fields for the provided URL.

+ (NSArray *)cookiesWithResponseHeaderFields:(NSDictionary *)headerFields forURL:(NSURL *)theURL

Parameters of [NSHTTPCookie cookiesWithResponseHeaderFields]
headerFields
The header fields used to create the NSHTTPCookie objects.
theURL
The URL associated with the created cookies.

Return Value
The array of created cookies.

Discussion of [NSHTTPCookie cookiesWithResponseHeaderFields]
This method ignores irrelevant header fields in headerFields, allowing dictionaries to contain additional data.

If headerFields does not specify a domain for a given cookie, the cookie is created with a default domain value of theURL.

If headerFields does not specify a path for a given cookie, the cookie is created with a default path value of “/”.

NSHTTPCookie cookiesWithResponseHeaderFields example.
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSHTTPURLResponse *)response {
    NSURL* redirected_url = [request URL];
    NSString* querystr = [redirected_url absoluteString];

    if (response != nil) {
        NSArray* zzzz = [NSHTTPCookie
                         cookiesWithResponseHeaderFields:[response allHeaderFields]
                         forURL:[NSURL URLWithString:@""]];

        if ([zzzz count] > 0) {
            if ([querystr isEqualToString:@"https://www.localhost.com/specificurl.aspx"]) {
                NSMutableArray* actualCookies = [[NSMutableArray alloc] init];
                NSUInteger i, count = [zzzz count];
                for (i = 0; i < count; i++) {
                    NSHTTPCookie* xxx = [zzzz objectAtIndex:i];
                    [actualCookies addObject:xxx];
                }

                NSHTTPCookie* obj = [self.tmpCookies objectAtIndex:0];
                [actualCookies addObject:obj];

                NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:actualCookies];

                NSURL *url = [NSURL URLWithString:@"https://www.localhost.com/specificurl.aspx"];
                NSMutableURLRequest* xrequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

                [xrequest setHTTPMethod:@"GET"];
                [xrequest setAllHTTPHeaderFields:headers];
                [xrequest setValue:@"https://www.localhost.com/Default.aspx?Site_ID=500000" forHTTPHeaderField: @"Referer"];

                [viewController setAuthCookieAfterValidLogin:zzzz];

                return xrequest;
            }
        }
    }

    return request;
}

Example of [NSHTTPCookie cookiesWithResponseHeaderFields].
- ( void )connection: (NSURLConnection *)connection
          didReceiveResponse: (NSURLResponse *)response
{
    NSHTTPURLResponse        *httpResponse = (NSHTTPURLResponse *)response;
    NSArray                  *cookies;

    cookies = [ NSHTTPCookie cookiesWithResponseHeaderFields:
                             [ httpResponse allHeaderFields ]];
    [[ NSHTTPCookieStorage sharedHTTPCookieStorage ]
            setCookies: cookies forURL: self.url mainDocumentURL: nil ];
}

NSHTTPCookie cookiesWithResponseHeaderFields example.
Assuming you have an NSHTTPURLResponse, you can get an array of cookies like so:

NSDictionary * headers = [(NSHTTPURLResponse *)response allHeaderFields];
NSArray * cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:headers forURL:response.URL];
Where response is the NSHTTPURLResponse.

You're going to get NSURLResponses in these 2 methods of the NSURLConnectionDelegate

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse;
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
NSHTTPCookie has a properties property which returns an NSDictionary, so you can save them. They can then be created with the same dictionary using -initWithProperties:

To send them, you'll need to create your own string for the Cookie header of a NSURLRequest. Something like:

NSMutableString * cookieString = [[NSMutableString alloc] init];
for (NSHTTPCookie * cookie in myLoadedCookies){
    [cookieString appendFormat:@"%@=%@; ", cookie.name, cookie.value];
}
[request setValue:cookieString forHTTPHeaderField:@"Cookie"];
[cookieString release];
Where request is an NSMutableURLRequest.

You should also make sure to stop iOS managing cookies itself:

[request setHTTPShouldHandleCookies:NO];

End of NSHTTPCookie cookiesWithResponseHeaderFields example article.