iOS Cookie学习(NSHTTPCookieStorage的使用)

最近也是忙里偷闲,无意中发现了一位大牛的文章,小小的研究了一下HTTP.今天先写一点Cookie,之前有过文章都是关于Cookie的,也没有深入研究,今天也算是一起学习吧.


@interface FQWebGlobalCookies : NSObject

/*
 全局单例
 */
+ (id)globalCookiesManager;

/*
 对每份URL产生的Cookie 保存起来,以便管理
 对响应头域中的cookie进行保存
 */
- (void)storeResponseCookies:(NSDictionary *)headerFields forURL:(NSURL *)requrl;

/*
 通过requrl获取对应的cookie
 返回url所有对应的cookie
 */
- (NSArray *)getCookiesByURL:(NSURL *)requrl;



/*
 通过getCookiesByURL提取出的cookies 解释出可以被请求头域Cookie使用的字符值
 返回一个供Cookie头域使用的值,如果提取不到返回nil
 */
- (NSString *)makeCookieValueOfHeaderByCookies:(NSArray *)cookies;
- (NSString *)makeCookieValueOfHeaderByURL:(NSURL *)requrl;


/*
 通过URL清除Cookie
 */
- (void)deleteCookiesByURL:(NSURL *)requrl;
/*
 所有URL
 */
- (NSArray *)allURL;

@end



@interface FQWebGlobalCookies()
{
    NSMutableDictionary     *cookiesDictionary;
}
@end

@implementation FQWebGlobalCookies

static FQWebGlobalCookies *globalCookiesInstance = nil;

+(id)globalCookiesManager{
    if (!globalCookiesInstance) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            globalCookiesInstance = [[FQWebGlobalCookies alloc]init];
        });
    
    }
    return globalCookiesInstance;
}

- (id)init{
    self = [super init];
    if (self) {
        cookiesDictionary = [[NSMutableDictionary alloc]init];
    }
    return self;
}

- (void)dealloc
{
    cookiesDictionary = nil;
}

/*
 对每份URL产生的Cookie 保存起来,以便管理
 对响应头域中的cookie进行保存
 */
- (void)storeResponseCookies:(NSDictionary *)headerFields forURL:(NSURL *)requrl{
    @synchronized(cookiesDictionary){
    
        NSArray *newCookies = [NSHTTPCookie cookiesWithResponseHeaderFields:headerFields forURL:requrl];
        
        if ([newCookies count] > 0) {
            NSHTTPCookie *cookie;
            NSMutableArray *globalCookies = [[NSMutableArray alloc]init];
            for (cookie in newCookies) {
                [globalCookies addObject:cookie];
            }
            
            // //如果URL相同会把旧的替换掉
            if ([globalCookies count] > 0)
            {
                [cookiesDictionary setObject:globalCookies forKey:requrl];
            }
            
            [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:newCookies forURL:requrl mainDocumentURL:nil];
        }
        
    }
}

/*
 通过requrl获取对应的cookie
 返回url所有对应的cookie
 */
- (NSArray *)getCookiesByURL:(NSURL *)requrl{

    NSArray *cookies = nil;
    @synchronized(self){
        cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage]cookiesForURL:[requrl absoluteURL]];
    }
    
    return cookies;
    
}

/*
 通过getCookiesByURL提取出的cookies 解释出可以被请求头域Cookie使用的字符值
 返回一个供Cookie头域使用的值,如果提取不到返回nil
 */
- (NSString *)makeCookieValueOfHeaderByCookies:(NSArray *)cookies{
    NSString *cookieHeader = nil;
    @synchronized(self){
        if ([cookies count] > 0) {
            NSHTTPCookie *cookie;
            for (cookies in cookies) {
                if (!cookieHeader) {
                     cookieHeader = [NSString stringWithFormat:@"%@=%@",[cookie name],[cookie value]];
                } else{
                cookieHeader = [NSString stringWithFormat: @"%@; %@=%@",cookieHeader,[cookie name],[cookie value]];
                }
            }
        }
    }
    return cookieHeader;
}

- (NSString *)makeCookieValueOfHeaderByURL:(NSURL *)requrl{
    NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[requrl absoluteURL]];
    return [self makeCookieValueOfHeaderByCookies:cookies];
    
}

/*
 通过URL清除Cookie
 */
- (void)deleteCookiesByURL:(NSURL *)requrl{
    @synchronized(self){
        NSArray *cookies = [cookiesDictionary objectForKey:requrl];
        for (NSHTTPCookie *cookie in cookies)
        {
            [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
        }
        
        [cookiesDictionary removeObjectForKey:requrl];
    }
}

/*
 所有URL
 */
- (NSArray *)allURL
{
    NSArray *urls = nil;
    @synchronized (cookiesDictionary){
        urls = [cookiesDictionary allKeys];
    }
    return urls ? urls : [NSArray array];
}
@end

iOS Cookie学习(NSHTTPCookieStorage的使用),,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。