Reference: http://blog.csdn.net/zhaoxy_thu/article/details/20532879
There is actually no code specifically for cookie encapsulation in afnetworking, but because it uses nsurlrequest at the bottom, cookies can be manipulated through nshttpcookiestorage.
Read and save cookies:
Nsarray *cookies = [[Nshttpcookiestorage sharedhttpcookiestorage] Cookiesforurl:[nsurl URLWithString:[BESTHttpClient Sharedclient].url_login]];
NSData *cookiedata = [Nskeyedarchiver archiveddatawithrootobject:cookies];
[[Nsuserdefaults Standarduserdefaults] Setobject:cookiedata Forkey:best_login_cookie];
LOG: (diary style of Cookies)
Cookies--(
<nshttpcookie version:0 Name: "Csrftoken" Value: "AX5XDE7MBBOCDPJSCMHYL8RMPK3M4YMB" expiresdate:2017-08-10 05:16:54 +0000 created:2016-08-11 05:16:54 +0000 sessiononly:false domain: "192.168.1.47" Path: "/" ISSECURE:FALSE>
<nshttpcookie version:0 Name: "SessionID" Value: "M675fhggawh3w18vmmujjwb67kyr9ook" expiresdate:2016-09-10 05:16:54 +0000 created:2016-08-11 05:16:54 +0000 sessiononly:false domain: "192.168.1.47" Path: "/" ISSECURE:FALSE>
)
To delete a cookie:
NSData *cookiedata = [[Nsuserdefaults standarduserdefaults] Objectforkey:best_login_cookie];
Nsarray *cookies = [Nskeyedunarchiver unarchiveobjectwithdata:cookiedata];
NSLog (@ "Cookies-%@", cookies);
For (Nshttpcookie *cookie in cookies) {
[[Nshttpcookiestorage Sharedhttpcookiestorage] Deletecookie:cookie];
}
Set Cookies:
NSData *cookiedata = [[Nsuserdefaults standarduserdefaults] Objectforkey:best_login_cookie];
Nsarray *cookies = [Nskeyedunarchiver unarchiveobjectwithdata:cookiedata];
For (Nshttpcookie *cookie in cookies) {
[[Nshttpcookiestorage Sharedhttpcookiestorage] Setcookie:cookie];
}
Note:
1. Save cookies by using the above method, you can use cookies when you exit the program and do not log in at the next entry.
2. Cookies set by the above method can be shared using the Loadrequest method of the WebView load Nsurlrequest.
Reading and setting cookies in IOS afnetworking