在 UIWebView 中启用 Cookie

时间:2023-03-22
本文介绍了在 UIWebView 中启用 Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

如何在使用 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

Set-Cookie: .ASPXAUTH=really-long-hex-number

它返回一个重定向到类似的东西

it returns a redirection to something like

位置:/(F(long-sorta-base64ish-looking-string))/

默认的 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 会发送如下内容:

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

尽早执行以下操作,可能在您的 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 中启用 Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:URLForUbiquityContainerIdentifier: 应该在主线程之外的线程中调用吗? 下一篇:iPhone GPS - 电池消耗极快

相关文章