Skip to content

Instantly share code, notes, and snippets.

@jxson
Created January 21, 2011 00:20
Show Gist options
  • Save jxson/789006 to your computer and use it in GitHub Desktop.
Save jxson/789006 to your computer and use it in GitHub Desktop.
Enable Cookies in your phoneGap app and open http/ https links in safari
- (id) init
{
/**
* Enable cookies
**/
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
[cookieStorage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
NSHTTPCookie *cookie;
for (cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage]
cookies]) {
NSLog(@"%@", [cookie description]);
}
return [super init];
}
- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
if ([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"])
{
[[UIApplication sharedApplication] openURL:url];
return NO;
}
else
{
return [ super webView:theWebView
shouldStartLoadWithRequest:request
navigationType:navigationType ];
}
}
@timtrautmann
Copy link

It looks like NSHTTPCookieAcceptPolicyAlways is the default policy in iOS5. Setting this doesn't seem to have the intended effect for me. My app still doesn't seem to be able to store cookies. Any ideas what else can be done?

@jxson
Copy link
Author

jxson commented Dec 9, 2011

hey @bringo, this was an old snippet. I have not tried getting cookies working in a webview in iOS5 and the code above was kind of a hack to get cookies to work the way I wanted in the phone gap project.

@timtrautmann
Copy link

Thanks for letting me know. Looks like they added this to PhoneGap directly... not that it is actually doing what it is supposed to for my app. :(
https://github.com/callback/callback-ios/blob/master/PhoneGapLib/Classes/PhoneGapDelegate.m#L64

@jxson
Copy link
Author

jxson commented Dec 9, 2011

I know your pain, good luck with your app!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment