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.