Question

I'm trying to do the following: Inside my appDelegate I'm setting a cookie. After that I'm trying to read that cookie using JavaScript from a webapp. Is this possible? Because I can't make it work...

This is my code in the iOS app:

NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
[cookieProperties setObject:@"test" forKey:NSHTTPCookieName];
[cookieProperties setObject:@"yes" forKey:NSHTTPCookieValue];
[cookieProperties setObject:self.siteURL forKey:NSHTTPCookieDomain];
[cookieProperties setObject:self.siteURL forKey:NSHTTPCookieOriginURL];
[cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];
[cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion];

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

NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie]; 

And in my JavaScript I'm using the jquery.cookie plugin for writing and reading cookies. But nothing is there... This is my code:

cookieValue = $.cookie("test");
alert(cookieValue);

The alert has in the body "[object Object]" and there should be the "yes" value I set in my app.

Do you guys think this is possible?

Thanks!

Was it helpful?

Solution

Not possible: the global Safari cookie cache isn't accessible to third-party applications, as that would be a significant security hole. If you need to send cookie information to Safari you'd be better served launching a URL into Safari with the cookie details as request parameters and then having the script at the end of that URL create the cookies and send them back.

EDIT: If you want to be slick you can register a URL handler for your iOS app and then launch the URL to your web app. Have your web app set the cookies, then redirect back to the app URL.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top