dzenbot / DZNWebViewController

A simple web browser for iPhone & iPad with similar features than Safari's
https://www.cocoacontrols.com/controls/dznwebviewcontroller
MIT License
1.06k stars 181 forks source link

Add Cookies to request #22

Closed nneuberger1 closed 9 years ago

nneuberger1 commented 9 years ago

I'm trying to use this control and add additional cookies to the requests so I can use it for authentication of a website.

How can this be done? I've tried adding code like this (and it's working with a default UIWebView), but with the DZNWebViewController, it's not working. I've tried initiating this code before or after the call to create DZNWebViewController.

[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];

///////////////////////////////

NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];

[cookieProperties setObject:@"mem_websiteId" forKey:NSHTTPCookieName];

[cookieProperties setObject:email forKey:NSHTTPCookieValue];
[cookieProperties setObject:@"www.yourdomainname.com" forKey:NSHTTPCookieDomain];
[cookieProperties setObject:@"www.yourdomainname.com" forKey:NSHTTPCookieOriginURL];
[cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];
[cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion];

[cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires];

NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];

[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
dzenbot commented 9 years ago

Have you seen this answer? http://stackoverflow.com/questions/26573137/can-i-set-the-cookies-to-be-used-by-a-wkwebview/26577303#26577303

I've never had to inject cookies so far, so I wouldn't exactly know. Might be interesting to give support to DZNWebViewController for doing it tho.

dzenbot commented 9 years ago

You might want to have a look at NSURLProtocol, so you can proxy the requests and append cookies for each, in a decoupled way.