IOS Reading notes-cookie

Source: Internet
Author: User

What is a cookie?
Cookie/session mechanism details The blogger writes in detail and can look at it.
Cookies can be conveniently used in an IOS app in an HTTP connection. The URL loading framework helps us do a lot of work to take advantage of this feature of the Protocol.
There are 3 places where cookies are used frequently: retrieving the value of a cookie, displaying a delete cookie, and manually adding a cookie to the request.
The URL loading system automatically processes all HTTP/S requests for cookies, stores the returned cookie in the response, and then adds it to the subsequent request in accordance with the cookie processing rules.
The URL loading system provides two important objects to manage Cookie:nshttpcookie and nshttpcookiestorage.
Nshttpcookie is an attribute that indicates that Cookie,nshttpcookiestorage is a singleton object that is used to manage the cookie of an app. The sandbox mode of iOS is not shared in the cookie app.

typedef NS_ENUM(NSUInteger, NSHTTPCookieAcceptPolicy) {    //默认值保存所有返回cookie    NSHTTPCookieAcceptPolicyNever,//不保存cookie    NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain//只保存与请求域相匹配的cookie};

Instance:

[[NSHTTPCookieStorage sharedHTTPCookieStorage]setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyNever];

The code can also stop using automated cookie processing for each request, which is done by invoking the

[request setHTTPShouldHandleCookies:NO];//request为NSMutableURLRequest对象

This organizes the URL loading system to process the returned requests.

1. Obtain a cookie from the response

It is common for God to get a cookie from the response and then find a specific cookie by name.

 Nsurl*url = [Nsurlurlwithstring:@"Http://sale.jd.com/act/yRkab6FWMOlUV.html?cpdad=1DLSUE"];    Nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url]; Nshttpurlresponse *response;Nserror*error;if(Error) {NSLog(@"Error occurred"); } NSData *data = [nsurlconnectionSendsynchronousrequest:request Returningresponse:&response error:&error];nsdictionary*headers = [response allheaderfields];//Get all key values for the response header    NSLog(@"headers =%@", headers);Nsarray*cookies = [Nshttpcookie cookieswithresponseheaderfields:headers forurl:url];NSLog(@"Cookiearray =%@", cookies); for(Nshttpcookie *cookie in cookies) {if([[Cookie name] isequaltostring:@"1"]) {        }    }

2 Deleting cookies

 NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];    //获取保存的cookie    NSArray *cookiesArray = [cookieStorage cookies];    //获取指定url 的cookie    NSArray *urlCookies = [cookieStorage cookiesForURL:url];    for (NSHTTPCookie * cookie in cookiesArray) {        [cookieStorage deleteCookie:cookie];    }

3 Creating a cookie

key value of//cookie    nsdictionary*dic = [nsdictionaryDictionarywithobjectsandkeys: @"Foo", Nshttpcookiename, @"This is foo", Nshttpcookievalue, @"/", Nshttpcookiepath, Url,nshttpcookieoriginurl,Nil];//Initialize cookies with a dictionaryNshttpcookie *cookie = [Nshttpcookie cookiewithproperties:dic]; Nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url];//Put the cookie into the array    Nsarray*newcookie = [NsarrayArraywithobject:cookie];NSLog(@"Old Headler =%@", [request Allhttpheaderfields]);//Convert a cookie array into a dictionary    nsdictionary*newheaders = [Nshttpcookie Requestheaderfieldswithcookies:newcookie];//Replace request header[Request Setallhttpheaderfields:newheaders];NSLog(@"New Headler =%@", [request Allhttpheaderfields]); [nsurlconnectionSendsynchronousrequest:request Returningresponse:&response error:&error];

To this end ....
The article is from the advanced programming of iOS Network

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

IOS Reading notes-cookie

Related Article

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.