Wednesday, June 12, 2013

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.