简体   繁体   中英

Unique Identifier for both mobile safari and in app in iOS

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM