How to get UIWebView User-Agent(如何获取 UIWebView 用户代理)
问题描述
我在使用一台远程服务器时遇到了问题.我的应用程序使用 [NSData initWithContentsOfURL:] 方法向服务器发出请求,作为响应,我得到了在
UIWebView
中打开的网站 url.
I've got a problem working with one remote server. My app makes a request to a server using [NSData initWithContentsOfURL:]
method and as a response I get website's url which I open in UIWebView
.
问题是这些请求具有不同的用户代理,服务器无法正确地为我服务,因为它希望我使用相同的用户代理发送所有请求.我知道如何更改用户代理(例如 更改 UIWebView 中的用户代理(iPhone SDK)) 但我真正想要的是以某种方式获取 UIWebView
的 User-Agent 并将其设置为 [NSData initWithContentsOfURL:] 以避免服务器端出现问题
The problem is that those requests have different User-Agent and server can't serve me correct because it expects that I send all requests with the same User-Agent. I know how to change User-Agent (e.g Change User Agent in UIWebView (iPhone SDK)) but what I really want it is somehow to get UIWebView
's User-Agent and set it to [NSData initWithContentsOfURL:] to avoid problems with server side
推荐答案
我刚刚遇到了类似的问题,需要使 NSURLConnection 发送的用户代理与 UIWebView 发送的用户代理匹配.我的解决方案是创建一个 UIWebView,然后使用 javascript 拉出用户代理.
I just ran into a similar issue and needed to make the user agent sent by an NSURLConnection match the one sent by a UIWebView. My solution was to create a UIWebView and then just use javascript to pull out the user agent.
UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectZero];
NSString* secretAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
这篇关于如何获取 UIWebView 用户代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何获取 UIWebView 用户代理


基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01