iOS cookie的三种用法

在APP开发中,cookie也开始变的越来越重要了。为了更好的切换,注销和登录账户,就必须熟悉cookie的用法。

花了点时间把代码整理了一下,注释也都标上了

1、获取cookie

获取cookie只能在请求中获取cookie,否则时获取不到的,url就不给出了,大家用自己的url测试一下就行。

获取到cookie后把cookie进行归档保存到userDefaults里

#pragma mark 获取并保存cookie到userDefaults

- (void)getAndSaveCookie {
    
    NSLog(@"=============获取cookie==============");
    NSString *urlString = @"";
    
    //请求一个网址,即可分配到cookie
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer new];
    [manager GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        //获取cookie
        NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
        for (NSHTTPCookie *tempCookie in cookies) {
            //打印获得的cookie
            NSLog(@"getCookie: %@", tempCookie);
        }
        
        /*
         * 把cookie进行归档并转换为NSData类型
         * 注意:cookie不能直接转换为NSData类型,否则会引起崩溃。
         * 所以先进行归档处理,再转换为Data
         */
        NSData *cookiesData = [NSKeyedArchiver archivedDataWithRootObject: [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]];
        //存储归档后的cookie
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setObject: cookiesData forKey: @"cookie"];
        
        
        [self deleteCookie];
        [self setCoookie];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
}

2、删除cookie

把获取到的cookie删除掉,该步骤可以用在注销或者切换账号里。

当前,我这里删除cookie是为了检测后面的通过本地存储的数据进行设置cookie是否成功

#pragma mark 删除cookie
- (void)deleteCookie {
    
    NSLog(@"============删除cookie===============");
    
    NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
    
    
    //删除cookie
    for (NSHTTPCookie *tempCookie in cookies) {
        
        [cookieStorage deleteCookie:tempCookie];
    }
    
    
    //把cookie打印出来,检测是否已经删除
    NSArray *cookiesAfterDelete = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
    for (NSHTTPCookie *tempCookie in cookiesAfterDelete) {
        
        NSLog(@"cookieAfterDelete: %@", tempCookie);
    }
    NSLog(@"\n");
    
}

3、通过本地存储的数据设置cookie

把本地的cookie取出并反归档,设置到cookie中,并且检测cookie是否设置成功

#pragma mark 再取出保存的cookie重新设置cookie

- (void)setCoookie {
    
    NSLog(@"============再取出保存的cookie重新设置cookie===============");
    
    //取出保存的cookie
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    //对取出的cookie进行反归档处理
    NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData:[userDefaults objectForKey:@"cookie"]];
    if (cookies) {
        NSLog(@"有cookie");
        //设置cookie
        NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
        for (id cookie in cookies) {
            [cookieStorage setCookie:(NSHTTPCookie *)cookie];
        }
    }else{
        NSLog(@"无cookie");
    }
    
    
    //打印cookie,检测是否成功设置了cookie
    NSArray *cookiesA = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
    for (NSHTTPCookie *cookie in cookiesA) {
        
        NSLog(@"setCookie: %@", cookie);
    }
    NSLog(@"\n");
    
}

参考:http://www.jianshu.com/p/cbbce1627e03

你可能感兴趣的:(iOS cookie的三种用法)