我正在构建一个带有cookie的iPhone应用程序.在Safari设置中删除Cookie不会删除它们.它们存放在哪里?是否可以从另一个UIWebView读取它们?
谢谢!
苦苦于解决这个问题并且不愿实施自定义cookie管理系统.
看来iOS的一些隐藏级别的HTTP实现无法正确管理无会话cookie.每当HTTP响应设置或删除cookie时,立即检查NSHTTPCookieStorage cookie将产生预期结果并指示正确的sessionOnly值.
但是,如果应用程序在响应更新cookie后立即退出,则重新启动时,这些sessionOnly = FALSE cookie将恢复为某个先前状态,并且最新更新将丢失.
是否通过响应头或NSHTTPCookieStorage setCookie设置/删除cookie:没有区别.
一些缓存/同步伏都教必须在幕后进行.cookie变为持久化所需的时间最长可达5秒.
在那里有谁或者可以指出这种行为的明确解释?这是一个简单明了的bug吗?还是一些我无法理解的无证特征?
您可以使用一些代码重现:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
NSHTTPCookie *cookie;
for (cookie in [NSHTTPCookieStorage sharedHTTPCookieStorage].cookies) {
NSLog(@"%@=%@", cookie.name, cookie.value);
}
NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
[cookieProperties setObject:@"testCookie" forKey:NSHTTPCookieName];
[cookieProperties setObject:[NSString stringWithFormat:@"%f", [[NSDate date] timeIntervalSince1970]] forKey:NSHTTPCookieValue];
[cookieProperties setObject:@"www.example.com" forKey:NSHTTPCookieDomain];
[cookieProperties setObject:@"www.example.com" forKey:NSHTTPCookieOriginURL];
[cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];
[cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion];
// set expiration to one month from now
[cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires];
cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}
Run Code Online (Sandbox Code Playgroud)
此代码应在每次启动时输出新值.相反,您会看到如果您快速退出应用程序,则值将保持不变. …
我正在开发报摊应用程序并使用NSURLRequest下载问题资产.
NSArray *contents = [issue.tableOfContents objectForKey:kSNTableOfContentsContents];
NSHTTPCookie *cookie;
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSLog(@"HERE GO MY COOKIES");
for (cookie in [cookieJar cookies]) {
NSLog(@"%@", cookie);
}
for (NSDictionary *contentItem in contents) {
NSString *contentURL_string = [contentItem objectForKey:kSNTableOfContentsRemoteURL];
NSURL *contentURL = [NSURL URLWithString:contentURL_string];
NSString *fileName = [contentItem objectForKey:kSNTableOfContentsContentsURL];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:contentURL];
NKAssetDownload *asset = [newsstandIssue addAssetWithRequest:request];
[request release];
....
[asset downloadWithDelegate:self];
....
}
Run Code Online (Sandbox Code Playgroud)
执行第一个for循环时,我的cookie似乎在NSHTTPCookieStorage中,但是当发送实际请求时,标头中没有cookie信息.我使用CharlesProxy来查看它.有谁可以请一些可能导致这个问题的建议?
我在MobileSafari中加载了一个页面,它通过CORS与另一台服务器通信.
在桌面浏览器(经过测试的Chrome和Safari)中,我可以登录,获取会话cookie,并将该会话cookie发回给后续请求,以便我可以通过所有API调用进行身份验证.
然而,当我通过移动Safari浏览器登录时,该cookie并没有得到发回后续请求.
我正在使用Charles Proxy监视正在发生的事情,它告诉我:
POST https://myremoteserver.com/sessions.json 传递我的登录信息Set-Cookie标头的响应.GET https://myremoteserver.com/checkout.json请求,没有Cookie请求标头.我正在使用此代码段Zepto.js以确保withCredentials: true在XHR对象上正确设置.(原谅coffeescript)
# Add withCredentials:true to the xhr object to send the remote server our cookies.
xhrFactory = $.ajaxSettings.xhr
$.ajaxSettings.xhr = ->
xhr = xhrFactory.apply(this, arguments)
xhr.withCredentials = yes
xhr
Run Code Online (Sandbox Code Playgroud)
并且该代码段在桌面浏览器中运行良好,在我添加之前,我无法在这些桌面浏览器中保留会话cookie.
MobileSafari中有一些怪癖阻止它像桌面浏览器一样工作吗?为什么它不能以同样的方式工作?
编辑!
这是我的rails 2.3应用程序中的我的CORS标题设置,我相信相当标准的东西
def add_cors_headers
if valid_cors_domain
headers['Access-Control-Allow-Origin'] = request.headers['HTTP_ORIGIN']
headers['Access-Control-Expose-Headers'] = 'ETag'
headers['Access-Control-Allow-Methods'] = 'GET, POST, PATCH, PUT, DELETE, OPTIONS, HEAD'
headers['Access-Control-Allow-Headers'] = '*,x-requested-with,Content-Type,If-Modified-Since,If-None-Match' …Run Code Online (Sandbox Code Playgroud) 我发现Vimeo iOS原生应用程序可以将cookie设置为移动Safari.它是怎么做的?
回购步骤:
1,在iOS设备上安装Vimeo iOS本机应用程序.
2,打开此Vimeo iOS原生应用程序,然后使用您的Vimeo帐户登录.
3,打开移动版Safari,然后打开网页"https://vimeo.com/".您发现自己没有登录Vimeo.
4,打开Vimeo iOS原生应用程序,然后单击左侧导航栏中的"帮助(问号)"图标.您将看到Vimeo的网页帮助中心是通过UIWebView打开的.然后你可以关闭这个帮助中心.
5,打开移动版Safari,然后打开网页"https://vimeo.com/".您发现自己已登录Vimeo.
谢谢.
我需要在swift中为webview设置一个cookie.我找到了一个解决方案,但它是针对objective-c的.怎么在Swift中做到这一点?
是否可以使用sharedHTTPCookieStorage为UIWebView手动设置cookie?
这是解决方案.
我想在Web视图中设置cookie.这有什么帮助吗?
NSUrl urlq = new NSUrl (url);
webview = new UIWebView ();
webview.LoadRequest(new NSUrlRequest(urlq));
webview.Frame = new RectangleF (0,0, webViewForLoad.Frame.Width, webViewForLoad.Frame.Height);
webview.AllowsInlineMediaPlayback = true;
//webview.LoadRequest (new NSUrl (url, false));
webview.ScalesPageToFit = true;
webViewForLoad.AddSubview (webview);
Run Code Online (Sandbox Code Playgroud) 我想,以节省会议UIWebView在提示如下此链接:
- (NSURLRequest*)addCookies:(NSArray *)cookies forRequest:(NSURLRequest *)request
{
NSMutableURLRequest *mutableRequest = [request mutableCopy];
if ([cookies count] > 0)
{
NSHTTPCookie *cookie;
NSString *cookieHeader = nil;
for (cookie in cookies)
{
if (!cookieHeader)
{
cookieHeader = [NSString stringWithFormat: @"%@=%@",[cookie name],[cookie value]];
}
else
{
cookieHeader = [NSString stringWithFormat: @"%@; %@=%@",cookieHeader,[cookie name],[cookie value]];
}
}
if (cookieHeader)
{
[mutableRequest setValue:cookieHeader forHTTPHeaderField:@"Cookie"];
}
return [mutableRequest copy];
}
return nil;
}
Run Code Online (Sandbox Code Playgroud)
并在viewDidLoad:
NSURLRequest *webRequest = [NSURLRequest requestWithURL:homeURL];
NSArray *cookies …Run Code Online (Sandbox Code Playgroud) ios ×6
iphone ×3
objective-c ×3
cookies ×2
ajax ×1
cocoa ×1
cocoa-touch ×1
coffeescript ×1
cors ×1
ipad ×1
javascript ×1
nshttpcookie ×1
nsurlrequest ×1
privacy ×1
swift ×1
uiwebview ×1
xamarin ×1
xamarin.ios ×1