Enable Cookies in UIWebView(在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 UIWebView 中启用 Cookie
基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01