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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With