Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a cookie in an iPhone App

Tags:

iphone

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?

like image 469
Brad The App Guy Avatar asked Oct 22 '09 03:10

Brad The App Guy


People also ask

Do iOS apps have cookies?

A normal iOS application does not contains cookies. An app will have cookies only if the application has one or more web views. To check where are the app cookies stored on iPhone, On an iPhone, go to Settings -> Safari -> Advanced -> Website Data and you will see all cookies stored on your device.

What is an iPhone cookie?

A cookie is a piece of data that a site puts on your device, so that site can remember you when you visit again. To choose whether Safari blocks cookies, tap Settings > Safari, then turn on Block All Cookies. If you block cookies, some web pages might not work.

What does it mean to enable cookies on iPhone?

Apple's websites and online services may use “cookies.” Cookies enable you to use shopping carts and to personalize your experience on our sites, tell us which parts of our websites people have visited, help us measure the effectiveness of ads and web searches, and give us insights into user behavior so we can improve ...


2 Answers

** 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!

like image 194
Shirkrin Avatar answered Nov 04 '22 13:11

Shirkrin


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.

like image 41
Andrew Johnson Avatar answered Nov 04 '22 14:11

Andrew Johnson