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