Reading of Cookies
Put it on the webviewdidfinishload after the start of execution
- Nsarray *ncookies = [[nshttpcookiestorage sharedhttpcookiestorage] cookies ]; Nshttpcookie *cookies;
- For (ID c in ncookies)
- {
- If ([C iskindofclass: [nshttpcookie class]]) {
- Cookies = (nshttpcookie *)c;
- NSLog(@"%@:%@", cookie. Name, cookie. Value);}
- }
2 Storing long -lived data
I want to read the cookie, in order to save the cookie, the next time you enter the APP, then take out to use, I wrote
The following two methods, can access the nsmutabledictionary type of data, this type of data, can be understood as key-value pairs, and it is convenient to read and modify a health value through the following two lines of code
- Read
- NSString *cookievalue = [cookiedata valueforkey:cookiename];
- Set / add
- Nsmutabledictionary *jsoncookie = [[nsmutabledictionary alloc] init];[ Jsoncookie setValue:cookievalue forkey:cookiename];
- Save data to Local
- - (BOOL) setlocaldata:(nsstring *)datafile dataObject:( Nsmutabledictionary *)dataObject{
- Set the path and save
- nsstring *savepath = [nssearchpathfordirectoriesindomains (nsdocumentdirectory,nsuserdomainmask , Yes) Objectatindex:< Span class= "lit" >0;
- NSString *saveFile = [savepath stringbyappendingpathcomponent:datafile];[ nskeyedarchiver archiverootobject:dataObject tofile:saveFile];
- return YES;
- }
- To read locally saved data
- - (nsmutabledictionary *) getlocaldata:(nsstring *)datafile {
- Read data by file name
- nsstring *savepath = [nssearchpathfordirectoriesindomains (nsdocumentdirectory,nsuserdomainmask , Yes) Objectatindex:< Span class= "lit" >0;
- NSString *saveFile = [savepath stringbyappendingpathcomponent:datafile]; return [nskeyedunarchiver unarchiveobjectwithfile: saveFile];
- }
Read and save cookies in IOS WebView-B