在UIWebView(iPhone)中启用Cookie [英] Enable Cookies in UIWebView (iPhone)
问题描述
如何在使用UIWebView窗口的iPhone应用程序中启用Cookie,以便我的登录系统可以正常工作?
How can I enable cookies in my iPhone Application that uses a UIWebView Window, so that my login system will work?
推荐答案
确保以
[NSHTTPCookieStorage sharedHTTPCookieStorage].cookieAcceptPolicy =
NSHTTPCookieAcceptPolicyAlways;
但是,如@JoelFan所述,问题可能是您的用户代理字符串导致ASP.NET尝试和失败在无Cookie登录。而不是包含
But, as mentioned by @JoelFan, the issue may be your User Agent string causing ASP.NET to attempt and fail at a cookieless login. Instead of a response that includes
的响应Set-Cookie:.ASPXAUTH = really-long-hex-number
它会传回重新导向至
位置:/(F( long-sorta-base64ish-looking-string ))/
默认UIWebView用户代理字符串是类似
The default UIWebView user agent string is something like
User-Agent:Mozilla / 5.0(iPad; CPU OS 7_0_2,如Mac OS X)AppleWebKit / 537.51.1(KHTML,like Gecko)Mobile / 11A501
User-Agent: Mozilla/5.0 (iPad; CPU OS 7_0_2 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Mobile/11A501
但ASP.NET不喜欢这样。 Safari会发送这样的东西:
but ASP.NET doesn't like this. Safari sends something like this:
User-Agent:Mozilla / 5.0(iPad; Mac OS X的CPU OS 7_0_2)AppleWebKit / 537.51。 1(KHTML,例如Gecko)版本/ 7.0 移动版/ 11A501 Safari / 9537.53
User-Agent: Mozilla/5.0 (iPad; CPU OS 7_0_2 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A501 Safari/9537.53
请在您的AppDelegate.m中进行以下操作:
Do the following early on, maybe in your AppDelegate.m
// DON'T try to reuse a UIWebView for this.
UIWebView *wv = [[UIWebView alloc] initWithFrame:CGRectZero];
// This webview has already decided to use the default user agent string.
// let's use javascript to get the existing user agent string
NSString *userAgent = [wv stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
// let's tack on some stuff to make ASP.NET happy
userAgent = [userAgent stringByAppendingString:@" Version/7.0 Safari/9537.53"];
[[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent": userAgent}];
// New UIWebViews inited after here will use the user agent string you made.
这篇关于在UIWebView(iPhone)中启用Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!