Reading and saving cookies in webview

Source: Internet
Author: User

It should be better to read the cookie and simply write the code segment here. It is easy to understand. You should put it in webviewdidfinishload and execute it

Nsarray * ncookies = [[nshttpcookiestorage sharedhttpcookiestorage] cookies]; nshttpcookie * cookie;
For (id c in ncookies)
{

If ([C iskindofclass: [nshttpcookie class]) {

Cookie = (nshttpcookie *) C;

Nslog (@ "% @: % @", Cookie. Name, Cookie. Value );}

}

Better storage of long-term data

I want to read the cookie. In order to save the cookie, I wrote

The following two methods can be used to access data of the nsmutabledictionary type. This type of data can be understood as a key-value pair, and you can easily read and modify a healthy 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 the data locally
-(Bool) setlocaldata :( nsstring *) datafile dataobject :( nsmutabledictionary *) dataobject {

// Set the path and save it

Nsstring * savepath = [nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes) objectatindex: 0];

Nsstring * SaveFile = [savepath stringbyappendingpathcomponent: datafile]; [nskeyedarchiver archiverootobject: dataobject tofile: SaveFile];
Return yes;

}

// Read locally stored data
-(Nsmutabledictionary *) getlocaldata :( nsstring *) datafile {

// Read data by file name

Nsstring * savepath = [nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes) objectatindex: 0];

Nsstring * SaveFile = [savepath stringbyappendingpathcomponent: datafile]; return [nskeyedunarchiver unarchiveobjectwithfile: SaveFile];

}

 

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.