Detailed approach to the management of cookies in IOS app development _ios

Source: Internet
Author: User

first, what is a cookie



Cookies are Web sites to be the terminal identity, stored in the terminal local user credentials information. The fields and meanings in the cookie are defined by the service side. For example, when a user logs on to a Web site, the server returns the cookie information to the terminal and the terminal saves the information, and the terminal sends the saved cookie message to the server the next time it visits the site. The server determines whether this user can log on automatically based on whether the cookie information is valid.



second, two classes of cookie management in iOS



The HTTP network request cookie Management in iOS is mainly handled by two classes, one class is Nshttpcookiestorage class and the other is Nshttpcookie class.



1.NSHTTPCookieStorage



The Nshttpcookiestorage class takes a single example of the design pattern, which manages cookie information for all HTTP requests, commonly used in the following ways:


Get a single Instance object
+ (Nshttpcookiestorage *) sharedhttpcookiestorage;
All cookie data arrays where Nshttpcookie objects are stored
@property (Nullable, ReadOnly, copy) Nsarray<nshttpcookie *> *cookies;
Manually set up a cookie data
-(void) Setcookie: (Nshttpcookie *) cookie;
Delete a cookie message
-(void) Deletecookie: (Nshttpcookie *) cookie;
Delete all cookie information after a time iOS8 available
-(Nullable Nsarray<nshttpcookie *> *) Cookiesforurl: (Nsurl *) URL;
Get all cookie data for a particular URL
-(void) Removecookiessincedate: (NSDate *) Date ns_available (10_10, 8_0);
Set a cookie for a specific URL
-(void) Setcookies: (Nsarray<nshttpcookie *> *) Cookies Forurl: (Nullable Nsurl *) URL Maindocumenturl: (Nullable Nsurl *) Maindocumenturl;
Receive protocol for cookie data
/*
Enumerated below:
typedef ns_enum (Nsuinteger, Nshttpcookieacceptpolicy) {
nshttpcookieacceptpolicyalways,//Receive all cookie information
nshttpcookieacceptpolicynever,//does not receive all cookie information
nshttpcookieacceptpolicyonlyfrommaindocumentdomain//only receive cookie information for the main document field
};
*/
@property Nshttpcookieacceptpolicy Cookieacceptpolicy;
The two notifications below the system are related to cookie management:


//Notification when receiving protocol of cookie data changes
FOUNDATION_EXPORT NSString * const NSHTTPCookieManagerAcceptPolicyChangedNotification;
//Notifications sent when managed cookie data changes
FOUNDATION_EXPORT NSString * const NSHTTPCookieManagerCookiesChangedNotification;



2.NSHTTPCookie



Nshttpcookie is a specific HTTP request cookie data object, where the property method is as follows:


The following two methods are used for object creation and initialization through the dictionary for key value setting
-(Nullable Instancetype) Initwithproperties: (nsdictionary<nsstring *, id> *) properties;
+ (Nullable Nshttpcookie *) Cookiewithproperties: (nsdictionary<nsstring *, id> *) properties;
Returns a dictionary in cookie data that can be used to add HTTP header fields
+ (nsdictionary<nsstring *, nsstring *> *) requestheaderfieldswithcookies: (Nsarray<nshttpcookie *> *) Cookies
Resolves the cookie data from the specified response header and URL address
+ (Nsarray<nshttpcookie *> *) Cookieswithresponseheaderfields: (nsdictionary<nsstring *, NSString *> *) Headerfields Forurl: (Nsurl *) URL;
Dictionary of properties in cookie data
@property (Nullable, ReadOnly, copy) nsdictionary<nsstring *, id> *properties;
The version of the request response
@property (readonly) Nsuinteger version;
Request the appropriate name
@property (readonly, copy) NSString *name;
Request the appropriate value
@property (readonly, copy) NSString *value;
Expiration time
@property (Nullable, ReadOnly, copy) NSDate *expiresdate;
The requested domain name
@property (readonly, copy) NSString *domain;
The path of the request
@property (readonly, copy) NSString *path;
Whether it is a secure transmission
@property (readonly, getter=issecure) BOOL secure;
Whether to send only HTTP services
@property (readonly, getter=ishttponly) BOOL HttpOnly;
The document for the response
@property (Nullable, ReadOnly, copy) NSString *comment;
The corresponding document URL
@property (Nullable, ReadOnly, copy) Nsurl *commenturl;
Service Port List
@property (Nullable, ReadOnly, copy) Nsarray<nsnumber *> *portlist;


Third, clear cookies
to clear all cookie methods:


NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"]; 
    if (url) { 
        NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:url]; 
        for (int i = 0; i < [cookies count]; i++) { 
            NSHTTPCookie *cookie = (NSHTTPCookie *)[cookies objectAtIndex:i]; 
            [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie]; 
             
        }  
To clear a particular cookie method:
NSArray * cookArray = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:self.loadURL]]; NSString * successCode = @""; for (NSHTTPCookie*cookie in cookArray) { if ([cookie.name isEqualToString:@"cookiename"]) { [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie]; } } 


To clear a URL cache method:
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:[NSURLRequest requestWithURL:url]]; 



Clear all Caching methods:
[[NSURLCache sharedURLCache] removeAllCachedResponses]; 


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.