13

I'm looking to uniquely identify an iPhone so our server can respond with some custom html. I want to be able to identify the phone in Safari and in an embedded browser in the app. Since each application is sandboxed, cookies won't work.

Thanks

2
  • Can you clarify what you mean by "uniquely identify" - do you mean you want to be able to tell when the same phone visits your web site either via an app or via Mobile Safari? Feb 8, 2012 at 16:21
  • yup I want to tell when a phone visits our site in app or in the mobile browser
    – Arvin Am
    Feb 8, 2012 at 16:22

2 Answers 2

12

This is not directly possible, but with the cooperation of the web site it is possible as Nick Lockwood has stated, and here's a greatly expanded version of the method he describes:

Your app should do this:

  1. Launch mobile safari, using [[UIApplication sharedApplication] openURL:url];
  2. The URL should be a special one, eg. http://yourwebsite.com/give-ios-app-the-cookie
  3. On your website, when that url is launched, issue a redirect to your-app-url-scheme:cookievalue=<somevalue> (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, http://yourwebsite.com/give-ios-app-the-cookie 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.

10

This is actually very hard.

You could program the app so that it launches Mobile Safari when it first opens and sets a cookie, then redirects back to the app using a custom URL scheme.

That may seem weird but it's actually how Facebook's shared login system works and it allows both the app and the browser to "break out" of their sandboxes and share the same unique cookie ID.

Of course you wouldn't know if the user had deleted the cookie unless you do the redirect every time the app launches, which might get annoying.

UPDATE:

As of iOS 9, you can now open the page using SFSafariViewController inside your app instead of redirecting to the Safari app. This is still a bit clunky, but may be preferable to a double app switch.

2
  • Great idea, but according to stackoverflow.com/questions/8636202/… the cookies are disabled by default in the Mobile Safari. Am I getting anything wrong (please excuse me for a newbie question, I am a real newbie to the iOS development)? Jun 5, 2012 at 13:16
  • 3rd party cookies are disabled, but first party (ones from the same domain as the page you are on) are enabled by default. Jun 6, 2012 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.