In app development, cookies are becoming more and more important. To better switch, log off and log in to your account, you must be familiar with cookie usage.
It took some time to tidy up the code, and the notes were all marked.
1, get cookies
To get a cookie can only be in the request to obtain a cookie, otherwise not get, the URL is not given, people use their own URL test on the line.
Cookies are archived and stored in userdefaults after obtaining a cookie
| 123456789101112131415161718192021222324252627282930313233343536373839 |
#pragma mark 获取并保存cookie到userDefaults- (void)getAndSaveCookie{ NSLog(@"=============获取cookie=============="); NSString *urlString = @""; //请求一个网址,即可分配到cookie AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer = [AFJSONResponseSerializer new]; [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id 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"]; NSLog(@"\n"); [self deleteCookie]; [self setCoookie]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { nil; }];} |
2. Delete Cookies
Delete the acquired cookie, which can be used to log out or switch accounts.
Currently, I delete the cookie here in order to detect if the cookie was successfully set by the locally stored data later
| 12345678910111213141516171819 |
#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, set cookies with locally stored data
Remove and archive local cookies, set into cookies, and detect if cookies are set successfully
| 12345678910111213141516171819202122232425262728 |
#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");} |
Check out the Run
Finally, the demo
Cookietext
Usage of IOS Cookies