9

Is it possible to set a cookie in an iPhone Application that persists, so that later when the user is in Mobile Safari, that cookie can be sent to a webserver?

Brad The App Guy
  • 16,255
  • 2
  • 41
  • 60
  • Here is the solution: http://stackoverflow.com/questions/5837702/nshttpcookiestorage-state-not-saved-on-app-exit-any-definitive-knowledge-docume/15633164#15633164 – Kuldeep Singh Mar 26 '13 at 09:26

4 Answers4

16

** Update 2017 **
A lot of changes to security mechanisms and cross-app communication were introduced to iOS in the recent years since this was first answered.

The below code no longer works on current iOS releases since Safari no longer accepts javascript:... in URLs and frameworks like NSURL catch these and return nil.

The one alternative that still works is to either host a website and have Safari open it or integrate such a HTML page in your app and run a small http server to host it on demand.

**iOS up to 6.x **
Since Apple has forced the sandboxing on all app store applications
there's currently no easy way to realize your request.

You could however open a special http://-URL from your application containing javascript to place a cookie:

NSString jsURL = @"javascript:function someFunction(){ /* your javascript code here */ } someFunction();void(0)";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: jsURL]];

Using javascript in URLs has been used by different iPhone applications to cross communicate
with MobileSafari (for example instapaper).

Another option would be to include a static HTML page in your app or on your server and instruct MobileSafari to open it.
The page in turn could set the permanent cookie.

Hope this helps!

Shirkrin
  • 3,993
  • 1
  • 29
  • 35
  • Tried this out and it doesn't seem to work. [NSURL URLWithString:@"javascript:function someFunction(){ alert('test!'); } someFunction();void(0)"] returns nil. – Pete Hodgson Jul 26 '10 at 21:25
  • Using the iOS Safari.app you can't (not without jailbreak and file:// support). – Shirkrin May 30 '11 at 08:56
  • javascript:// scheme does not seem to work, a least for iOS5/6 – ikarius Jul 27 '12 at 10:32
  • I would like to note that this answer is two years old and may not work with current iOS releases. As a matter of fact a bookmark with 'javascript:function()' still works – Shirkrin Aug 06 '12 at 09:21
  • @ikarius - there is no place in my answer where I mention a 'javascript://' scheme - it's 'javascript:function someFunction(){...}' -- this has to be used exactly like shown above – Shirkrin Aug 06 '12 at 09:22
  • This not working. Safari don't have javascript scheme! – Andrey M. Jun 02 '17 at 06:16
  • @Shirkrin Ok, then read the first comment. [NSURL URLWithString: jsURL] returns nil. I assume its not correct url string. just "javascript:alert()" not working too. – Andrey M. Jun 02 '17 at 08:22
  • @AndreyM. You're right, this seems to be part of the tighter security in iOS these days. Safari also no longer accepts URLs containing javascript:.. I updated the answer to include this fact. – Shirkrin Jun 02 '17 at 08:53
2

I believe this is made easy by using the ASIHTTPRequest Library. It encapsulates the use of the global cookie store.

http://allseeing-i.com/ASIHTTPRequest/How-to-use

You can make requests with this library which will accrue cookies, and then these cookies will affect other requests later.

I use this to great effect in accessing authenticated APIs within my iPhone app.

Andrew Johnson
  • 13,108
  • 13
  • 75
  • 116
0

The documentation for NSCookieStorage suggests that it would be such a mechanism. But whether "all applications" really includes Mobile Safari or not, your experimentation will have to determine....

See also the general documentation for the URL Loading System.

Sixten Otto
  • 14,816
  • 3
  • 48
  • 60
  • Based on my quick experiments, The cookies I was saving using NSCookieStorage were only readable by the app that created them. I'm not sure if I am doing something wrong or if that passage in the docs is misleading. – Brad The App Guy Oct 25 '09 at 22:48
  • I admit I've never actually used it in my own apps, but I did see comments on the web that seemed to indicate that others had had success with it. Sorry to hear that it didn't work for you. :-( – Sixten Otto Oct 25 '09 at 22:51
0

I'm new at iPhone development, but wouldn't opening a UIWebView allow your server to set a cookie on the browser so then when the user visits the site with Safari the cookie would be readable to your web server? Perhaps a hidden or small UIWebView? I can't tell what the use case is from your question.

Sandro
  • 4,761
  • 1
  • 34
  • 41