Wednesday, June 12, 2013

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.