I have data inside an array (NSHTTPCookieStorage)...
NSARRAY *httpCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:aHttpUrl];
I know for a fact that there are 3 cookies being stored (as I can see them in an NSLog).
I don't know how to obtain 1 specific cookies value based on the "name".
Example: <NSHTTPCookie version:0 name:\"username\" value:\"namehere\" expiresDate:2035-08-3 23:12:50 +0000 created:2015-08-08 23:12:49 +0000 (4.60768e+08) sessionOnly:FALSE domain:\"somedomain.com\" path:\"/example\" isSecure:FALSE>
...how do I get the contents of "value" so I can work with it as a string?
Like so
NSArray* cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:YOUR_NETWORK_URL]];
for (NSHTTPCookie* cookie in cookies)
{
NSLog(@"%@, ", [cookie valueForKey:@"version"]);
NSLog(@"%@, ", [cookie valueForKey:@"name"]);
NSString * this = [cookie valueForKey:@"name"];
NSLog(@"%@, ", [cookie valueForKey:@"value"]);
NSLog(@"%@, ", [cookie valueForKey:@"expiresDate"]);
NSLog(@"%@, ", [cookie valueForKey:@"created"]);
NSLog(@"%@, ", [cookie valueForKey:@"sessionOnly"]);
NSLog(@"%@, ", [cookie valueForKey:@"domain"]);
NSLog(@"%@, ", [cookie valueForKey:@"path"]);
NSLog(@"%@, ", [cookie valueForKey:@"isSecure"]);
}
or
three cookies,
NSString * this1 = [cookies[0] valueForKey:@"name"]
NSString * this2 = [cookies[1] valueForKey:@"name"]
NSString * this3 = [cookies[2] valueForKey:@"name"]
etc, etc, etc, do this will all cookies using key values
Manipulate cookies:
NSString *alipaySetCookieString = @"CAKEPHP=nil; path=#; domain=#; expires=Wed, 30-Nov-2001 01:01:01 GMT";
NSHTTPCookie * clok = [alipaySetCookieString cookie];
using this:
header for category "NSString+Cookie.h"
@interface NSString(Cookie)
- (NSHTTPCookie *)cookie;
@end
Implementation file for category "NSString+Cookie.m"
#import "NSString+Cookie.h"
@implementation NSString(Cookie)
- (NSDictionary *)cookieMap{
NSMutableDictionary *cookieMap = [NSMutableDictionary dictionary];
NSArray *cookieKeyValueStrings = [self componentsSeparatedByString:@";"];
for (NSString *cookieKeyValueString in cookieKeyValueStrings) {
NSRange separatorRange = [cookieKeyValueString rangeOfString:@"="];
if (separatorRange.location != NSNotFound &&
separatorRange.location > 0 &&
separatorRange.location < ([cookieKeyValueString length] - 1)) {
NSRange keyRange = NSMakeRange(0, separatorRange.location);
NSString *key = [cookieKeyValueString substringWithRange:keyRange];
NSString *value = [cookieKeyValueString substringFromIndex:separatorRange.location + separatorRange.length];
key = [key stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[cookieMap setObject:value forKey:key];
}
}
return cookieMap;
}
- (NSDictionary *)cookieProperties {
NSDictionary *cookieMap = [self cookieMap];
NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
for (NSString *key in [cookieMap allKeys]) {
NSString *value = [cookieMap objectForKey:key];
NSString *uppercaseKey = [key uppercaseString];
if ([uppercaseKey isEqualToString:@"DOMAIN"]) {
if (![value hasPrefix:@"."] && ![value hasPrefix:@"www"]) {
value = [NSString stringWithFormat:@".%@",value];
}
[cookieProperties setObject:value forKey:NSHTTPCookieDomain];
}else if ([uppercaseKey isEqualToString:@"VERSION"]) {
[cookieProperties setObject:value forKey:NSHTTPCookieVersion];
}else if ([uppercaseKey isEqualToString:@"MAX-AGE"]||[uppercaseKey isEqualToString:@"MAXAGE"]) {
[cookieProperties setObject:value forKey:NSHTTPCookieMaximumAge];
}else if ([uppercaseKey isEqualToString:@"PATH"]) {
[cookieProperties setObject:value forKey:NSHTTPCookiePath];
}else if([uppercaseKey isEqualToString:@"ORIGINURL"]){
[cookieProperties setObject:value forKey:NSHTTPCookieOriginURL];
}else if([uppercaseKey isEqualToString:@"PORT"]){
[cookieProperties setObject:value forKey:NSHTTPCookiePort];
}else if([uppercaseKey isEqualToString:@"SECURE"]||[uppercaseKey isEqualToString:@"ISSECURE"]){
[cookieProperties setObject:value forKey:NSHTTPCookieSecure];
}else if([uppercaseKey isEqualToString:@"COMMENT"]){
[cookieProperties setObject:value forKey:NSHTTPCookieComment];
}else if([uppercaseKey isEqualToString:@"COMMENTURL"]){
[cookieProperties setObject:value forKey:NSHTTPCookieCommentURL];
}else if([uppercaseKey isEqualToString:@"EXPIRES"]){
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
dateFormatter.dateFormat = @"EEE, dd-MMM-yyyy HH:mm:ss zzz";
[cookieProperties setObject:[dateFormatter dateFromString:value] forKey:NSHTTPCookieExpires];
}else if([uppercaseKey isEqualToString:@"DISCART"]){
[cookieProperties setObject:value forKey:NSHTTPCookieDiscard];
}else if([uppercaseKey isEqualToString:@"NAME"]){
[cookieProperties setObject:value forKey:NSHTTPCookieName];
}else if([uppercaseKey isEqualToString:@"VALUE"]){
[cookieProperties setObject:value forKey:NSHTTPCookieValue];
}else{
[cookieProperties setObject:key forKey:NSHTTPCookieName];
[cookieProperties setObject:value forKey:NSHTTPCookieValue];
}
}
if (![cookieProperties objectForKey:NSHTTPCookiePath]) {
[cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];
}
return cookieProperties;
}
- (NSHTTPCookie *)cookie{
NSDictionary *cookieProperties = [self cookieProperties];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
return cookie;
}
@end