WebView and cookies
WebView and cookies
- Subject: WebView and cookies
- From: Mirko Viviani <email@hidden>
- Date: Thu, 16 Jul 2015 12:37:09 +0200
Hi,
I’m using the following code for FaceBook authentication with custom cookie management (-setHTTPShouldHandleCookies:NO),
but recently it has stopped working with a web message ‘Cookies are not enabled’.
It does work if I set -setHTTPShouldHandleCookies:YES, but I don’t want to share cookies with Safari.
Is there a workaround for this?
Thank you.
- (void)handleCookiesForResponse:(NSURLResponse *)response;
{
if ([response respondsToSelector:@selector(allHeaderFields)]) {
// grab the cookies from the header as NSHTTPCookies and store them locally
NSDictionary *headers = [(NSHTTPURLResponse *)response allHeaderFields];
if (headers) {
NSURL *url = [response URL];
NSArray *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:headers
forURL:url];
if ([cookies count] > 0) {
[cookieStorage_ setCookies:cookies];
}
}
}
}
- (NSURLRequest *)addCookiesToRequest:(NSURLRequest *)request;
{
// override WebKit's usual automatic storage of cookies
NSMutableURLRequest *mutableRequest = [[request mutableCopy] autorelease];
[mutableRequest setHTTPShouldHandleCookies:NO];
// add our locally-stored cookies for this URL, if any
NSArray *cookies = [cookieStorage_ cookiesForURL:[request URL]];
if ([cookies count] > 0) {
NSDictionary *headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
NSString *cookieHeader = [headers objectForKey:@"Cookie"];
if (cookieHeader) {
[mutableRequest setValue:cookieHeader forHTTPHeaderField:@"Cookie"];
}
}
return mutableRequest;
}
--
Best regards,
Mirko
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden