NSHTTPCookieStorage Array - 解析价值

时间:2015-08-09 00:35:37

标签: ios objective-c cookies uiwebview nshttpcookie

我在数组中有数据(NSHTTPCookieStorage)......

NSARRAY *httpCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:aHttpUrl];

我知道存储了3个cookie(因为我可以在NSLog中看到它们)。

我不知道如何根据“名称”获取1个特定的cookie值。

示例:<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>

...如何获取“值”的内容,以便我可以将其作为字符串使用?

1 个答案:

答案 0 :(得分:0)

喜欢这样

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"]);
}

或 三个饼干,

 NSString * this1 =  [cookies[0] valueForKey:@"name"]
 NSString * this2 =  [cookies[1] valueForKey:@"name"]
 NSString * this3 =  [cookies[2] valueForKey:@"name"]

等等等,使用键值

执行所有cookie

操纵cookie:

NSString *alipaySetCookieString = @"CAKEPHP=nil; path=#; domain=#; expires=Wed, 30-Nov-2001 01:01:01 GMT";
NSHTTPCookie * clok  = [alipaySetCookieString cookie];

使用这个: 类别标题&#34; NSString + Cookie.h&#34;

@interface NSString(Cookie)


- (NSHTTPCookie *)cookie;

@end

类别&#34; NSString + Cookie.m&#34;

的实施文件
#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