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



你可能感兴趣的:(IOS AFN 处理cookie)