最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Set a cookie in a iOS app and read that cookie from Safari - Stack Overflow

matteradmin7PV0评论

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!

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!

Share Improve this question asked Apr 23, 2013 at 18:54 AndresAndres 11.8k13 gold badges54 silver badges90 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

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.

Post a comment

comment list (0)

  1. No comments so far