Can an iPhone xcode application read cookies previously stored by Safari Mobile?

Can an iPhone application read cookies previously stored by Safari Mobile?

To actually answer your question:


Mobile Safari’s cookies are not accessible from SDK apps. And each SDK app is given its own WebKit cache and cookie stores, so while cookies will persist within the same app, they aren’t accessible betweeen apps.

As of iOS 9 this is possible!

Use a sfSafariViewController.

You will need to setup:

  • A custom URL scheme in your app to receive cookie data.
  • The website you are getting cookies from will need to implement an API specific your app’s custom URL scheme, to redirect back to your app.

You can clone this repo which has a fully working demo of this.

Hope this helps,


There is actually an interesting way if you have access to a server url.

  1. In your app launch the server url with mobile safari.
  2. The target server url reads the cookie and redirects back to an app specific url (myapp://cookie=123)
  3. The app is then switched back and you can read that value from the url handler

It’s a little hacky as the app would switch mobile safari and then immediately switch back to the app. But, it is possible.

Note that on iOS 8, you’re probably better using Safari Password Sharing to solve some of the use cases that give rise to this problem.

This is not directly possible, but with the cooperation of the web site it is possible.

Read More:   How to mock an exported const in jest

To clarify, the user case is that an Objective C application wants to read the value of a cookie that has been set by a website in mobile safari. (ie. in particular, a UIWebView was not involved in setting the cookie)

Your app should do this:

  1. Launch mobile safari, using [[UIApplication sharedApplication] openURL:url];
  2. The URL should be a special one, eg.
  3. On your website, when that url is launched, issue a redirect to your-app-url-scheme:cookievalue= (eg. angrybirds:cookievalue=hh4523523sapdfa )
  4. when your app delegate receives – (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation process the url to get the cookie value

Note that you should not do this automatically when the application starts – the user will see the transfer to Mobile Safari and back, which is not a good user experience and Apple will reject your app (Apple also consider this to be “uploading user’s personal data to server without their prior consent”). It would be better to do it in response to the user, paying attention to the user experience – eg. wait for the user to hit a “login” button, then do it, and if the user is not logged into your website, should show the user the login screen within safari. If the user is logged in you could briefly show a “Automatically logging you in…” screen for a second or two in Safari before redirecting the user back.

There’s no way to get this to work with hotmail/gmail/etc of course – it needs to be your own website.

Credit goes to Unique Identifier for both mobile safari and in app in iOS for suggesting this kind of approach.

Read More:   Reading bytes from a JavaScript string

Because of sandboxing on the iPhone you don’t have access to Safari’s cookies. You can only access cookies created within your application – by an UIWebView for example.

Although you have asked the same question twice before, here’s one approach not yet mentioned…

This may be a little convoluted, but you can do Greasemonkey-esque things with a UIWebView. Something like this:

I’ve used this technique to enhance 3rd party pages in an iPhone app, but I’m not sure if it will read cookies from the same place as Safari mobile.

Worth a shot though?

Here’s my utils get/set cookie methods.

+(void)setCookie:(NSString *)key withValue:(NSString *)value {
    NSArray *keys = [NSArray arrayWithObjects:
                     NSHTTPCookieValue, nil];
    NSArray *objects = [NSArray arrayWithObjects:
                        [NSDate distantFuture],
                        value, nil];    
    NSDictionary *dict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
    NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:dict];
    NSHTTPCookieStorage *sharedHTTPCookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    [sharedHTTPCookieStorage setCookie:cookie];

+(NSString *)getCookie:(NSString *)key {
    NSHTTPCookieStorage *sharedHTTPCookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    NSArray *cookies = [sharedHTTPCookieStorage cookiesForURL:[NSURL URLWithString:@"YOURDOMAIN"]];
    NSEnumerator *enumerator = [cookies objectEnumerator];
    NSHTTPCookie *cookie;
    while (cookie = [enumerator nextObject])
        if ([[cookie name] isEqualToString:key]) 
            return [cookie value];
    return nil;

You might want to check

if ([[NSHTTPCookieStorage sharedHTTPCookieStorage] cookieAcceptPolicy] != NSHTTPCookieAcceptPolicyAlways) {
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];       

But apparently NSHTTPCookieStorage does not even hold cookies from the last request in the current application on iOS (rdar://8190706)

Read More:   Check whether a window is Popup or not?

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts