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

前端 未结 8 946
日久生厌
日久生厌 2020-11-28 10:05

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

8条回答
  •  天命终不由人
    2020-11-28 10:33

    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.

    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. 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= (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.

    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.

提交回复
热议问题