在UIWebView中启用Cookies [英] Enable Cookies in UIWebView
问题描述
如何在使用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,像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:
用户代理:Mozilla / 5.0(iPad; CPU OS 7_0_2,例如Mac OS X)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中启用Cookies的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!