Read and save cookies in IOS WebView-B

Source: Internet
Author: User

Reading of Cookies

Put it on the webviewdidfinishload after the start of execution

  1. Nsarray *ncookies = [[nshttpcookiestorage sharedhttpcookiestorage] cookies ]; Nshttpcookie *cookies;
  2. For (ID c in ncookies)
  3. {
  4. If ([C iskindofclass: [nshttpcookie class]]) {
  5. Cookies = (nshttpcookie *)c;
  6. NSLog(@"%@:%@", cookie. Name, cookie. Value);}
  7. }

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

  1. Read
  2. NSString *cookievalue = [cookiedata valueforkey:cookiename];
  3. Set / add
  4. Nsmutabledictionary *jsoncookie = [[nsmutabledictionary alloc] init];[ Jsoncookie setValue:cookievalue forkey:cookiename];
  5. Save data to Local
  6. - (BOOL) setlocaldata:(nsstring *)datafile dataObject:( Nsmutabledictionary *)dataObject{
  7. Set the path and save
  8. nsstring *savepath = [nssearchpathfordirectoriesindomains (nsdocumentdirectory,nsuserdomainmask , Yes) Objectatindex:< Span class= "lit" >0;
  9. NSString *saveFile = [savepath stringbyappendingpathcomponent:datafile];[ nskeyedarchiver archiverootobject:dataObject tofile:saveFile];
  10. return YES;
  11. }
  12. To read locally saved data
  13. - (nsmutabledictionary *) getlocaldata:(nsstring *)datafile {
  14. Read data by file name
  15. nsstring *savepath = [nssearchpathfordirectoriesindomains (nsdocumentdirectory,nsuserdomainmask , Yes) Objectatindex:< Span class= "lit" >0;
  16. NSString *saveFile = [savepath stringbyappendingpathcomponent:datafile]; return [nskeyedunarchiver unarchiveobjectwithfile: saveFile];
  17. }

Read and save cookies in IOS WebView-B

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.