IOS Cookie Learning (use of nshttpcookiestorage)

Source: Internet
Author: User

Recently also sneak in, accidentally found a Daniel's article, a small study of the HTTP. Write a little cookie today, before the article is about the cookie, nor in-depth study, today is also a study together.


@interface Fqwebglobalcookies:nsobject

/*
Global single case
*/
+ (ID) Globalcookiesmanager;

/*
Save the cookie generated for each URL in order to manage
Save the cookie in the Response header field
*/
-(void) Storeresponsecookies: (nsdictionary *) headerfields Forurl: (Nsurl *) Requrl;

/*
Obtain the corresponding cookie via Requrl
Returns all the corresponding cookies for the URL
*/
-(Nsarray *) Getcookiesbyurl: (Nsurl *) Requrl;



/*
Cookies extracted by Getcookiesbyurl explain the character values that can be used by the request header domain cookie
Returns a value that is used by the cookie header field if the extract does not return nil
*/
-(NSString *) Makecookievalueofheaderbycookies: (Nsarray *) cookies;
-(NSString *) Makecookievalueofheaderbyurl: (Nsurl *) Requrl;


/*
Clearing cookies by URL
*/
-(void) Deletecookiesbyurl: (Nsurl *) Requrl;
/*
All URLs
*/
-(Nsarray *) Allurl;

@end



@interface Fqwebglobalcookies ()
{
Nsmutabledictionary *cookiesdictionary;
}
@end

@implementation Fqwebglobalcookies

static fqwebglobalcookies *globalcookiesinstance = nil;

+ (ID) globalcookiesmanager{
if (!globalcookiesinstance) {
Static dispatch_once_t Oncetoken;
Dispatch_once (&oncetoken, ^{
Globalcookiesinstance = [[Fqwebglobalcookies alloc]init];
});

}
return globalcookiesinstance;
}

-(ID) init{
self = [super init];
if (self) {
Cookiesdictionary = [[Nsmutabledictionary alloc]init];
}
return self;
}

-(void) dealloc
{
Cookiesdictionary = nil;
}

/*
Save the cookie generated for each URL in order to manage
Save the cookie in the Response header field
*/
-(void) Storeresponsecookies: (nsdictionary *) headerfields Forurl: (Nsurl *) requrl{
@synchronized (cookiesdictionary) {

Nsarray *newcookies = [Nshttpcookie cookieswithresponseheaderfields:headerfields forurl:requrl];

if ([newcookies count] > 0) {
Nshttpcookie *cookie;
Nsmutablearray *globalcookies = [[Nsmutablearray alloc]init];
For (cookie in newcookies) {
[Globalcookies Addobject:cookie];
}

If the same URL will replace the old
if ([globalcookies count] > 0)
{
[Cookiesdictionary setobject:globalcookies Forkey:requrl];
}

[[Nshttpcookiestorage sharedhttpcookiestorage] setcookies:newcookies forurl:requrl MainDocumentURL:nil];
}

}
}

/*
Obtain the corresponding cookie via Requrl
Returns all the corresponding cookies for the URL
*/
-(Nsarray *) Getcookiesbyurl: (Nsurl *) requrl{

Nsarray *cookies = nil;
@synchronized (self) {
cookies = [[Nshttpcookiestorage sharedhttpcookiestorage]cookiesforurl:[requrl Absoluteurl]];
}

return cookies;

}

/*
Cookies extracted by Getcookiesbyurl explain the character values that can be used by the request header domain cookie
Returns a value that is used by the cookie header field if the extract does not return nil
*/
-(NSString *) Makecookievalueofheaderbycookies: (Nsarray *) cookies{
NSString *cookieheader = nil;
@synchronized (self) {
if ([cookie Count] > 0) {
Nshttpcookie *cookie;
For (cookie in cookies) {
if (!cookieheader) {
Cookieheader = [NSString stringwithformat:@ "%@=%@", [cookie Name],[cookie value]];
} else{
Cookieheader = [NSString stringWithFormat: @ "%@;%@=%@", Cookieheader,[cookie Name],[cookie value]];
}
}
}
}
return cookieheader;
}

-(NSString *) Makecookievalueofheaderbyurl: (Nsurl *) requrl{
Nsarray *cookies = [[Nshttpcookiestorage sharedhttpcookiestorage] Cookiesforurl:[requrl AbsoluteURL]];
return [self makecookievalueofheaderbycookies:cookies];

}

/*
Clearing cookies by URL
*/
-(void) Deletecookiesbyurl: (Nsurl *) requrl{
@synchronized (self) {
Nsarray *cookies = [Cookiesdictionary Objectforkey:requrl];
For (Nshttpcookie *cookie in cookies)
{
[[Nshttpcookiestorage Sharedhttpcookiestorage] Deletecookie:cookie];
}

[Cookiesdictionary Removeobjectforkey:requrl];
}
}

/*
All URLs
*/
-(Nsarray *) Allurl
{
Nsarray *urls = nil;
@synchronized (cookiesdictionary) {
URLs = [Cookiesdictionary AllKeys];
}
return URLs? URLs: [Nsarray array];
}
@end

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.