問題描述
如何在使用 UIWebView 窗口的 iPhone 應(yīng)用程序中啟用 cookie,以便我的登錄系統(tǒng)正常工作?
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 所提到的,問題可能是您的用戶代理字符串導(dǎo)致 ASP.NET 嘗試在無 cookie 登錄時(shí)失敗.而不是包含
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
Set-Cookie: .ASPXAUTH=really-long-hex-number
它返回一個(gè)重定向到類似的東西
it returns a redirection to something like
位置:/(F(long-sorta-base64ish-looking-string))/
默認(rèn)的 UIWebView 用戶代理字符串類似于
The default UIWebView user agent string is something like
用戶代理: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 會(huì)發(fā)送如下內(nèi)容:
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)Version/7.0 Mobile/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
盡早執(zhí)行以下操作,可能在您的 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.
這篇關(guān)于在 UIWebView 中啟用 Cookie的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!