IOS AFN 处理cookie

//           
          NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage]cookiesForURL:[NSURL URLWithString:url]];
//           for (NSHTTPCookie *tempCookie in cookies)
//           {
//               //打印cookies
//               NSLog(@"getCookie:%@",tempCookie);
//           }
          NSDictionary *Request = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
//           
           NSUserDefaults *userCookies = [NSUserDefaults standardUserDefaults];
           [userCookies setObject:[Request objectForKey:@"Cookie"] forKey:@"mUserDefaultsCookie"];
//           [userCookies synchronize];
//           NSURL * URL  = [NSURL URLWithString:url];
//           
//           NSArray  *cookies= [[NSHTTPCookieStorage sharedHTTPCookieStorage]cookiesForURL:URL];
//           
//           NSLog(@"首次登陆获得22cookies%@",cookies);
//           
//           
//           for (NSHTTPCookie *tempCookie in cookies)
//           {
//               // 打印cookies
//               NSLog(@"getCookie:%@",tempCookie);
//               
//           }
//
//           NSData *cookiesData = [NSKeyedArchiver archivedDataWithRootObject: [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]];
//           
//           //存储归档后的cookie
//           NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//           [userDefaults setObject: cookiesData forKey: @"cookie"];
//           NSLog(@"存储成功");
//           


    //self.manager
    
  //  [ self.manager.requestSerializer setValue:[[NSUserDefaults standardUserDefaults]objectForKey:@"mUserDefaultsCookie"]forHTTPHeaderField:@"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");
//    }






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



最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容