问题描述
是否可以在持续存在的iPhone应用程序中设置cookie,以便以后当用户在Mobile Safari中时,该cookie可以发送到网络服务器?
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?
推荐答案
** 2017年更新**
自从第一次回答以来,近年来对iOS引入了许多安全机制和跨应用程序通信的更改。
** 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.
以下代码不再适用于当前的iOS版本,因为Safari不再接受 javascript:...
in URL和框架如 NSURL
捕获这些并返回 nil
。
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
.
另一个可行的方法是主持一个网站并让Safari打开它或在你的应用程序中集成这样一个HTML页面并运行一个小的http服务器来按需托管它。
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高达6.x **
由于Apple已强制所有应用程序商店应用程序上的沙盒
目前没有轻松实现您的要求。
**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.
然而,你可以打开一个特殊的从包含javascript的应用程序放置一个cookie:
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]];
在URL中使用javascript已被不同的iPhone应用程序用于交叉通信
使用MobileSafari(例如)。
Using javascript in URLs has been used by different iPhone applications to cross communicate
with MobileSafari (for example instapaper).
另一种选择是在您的应用程序或服务器上包含一个静态HTML页面,并指示MobileSafari打开它。
页面依次可以设置永久性cookie。
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.
希望这会有所帮助!
这篇关于在iPhone应用程序中设置cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!