在 UIWebView 中更改用户代理 [英] Change User Agent in UIWebView

查看:24
本文介绍了在 UIWebView 中更改用户代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的业务需要能够为嵌入式 UIWebView 自定义 UserAgent.(例如,如果用户使用的是应用的一个版本而不是另一个版本,我希望服务器做出不同的响应.)

I have a business need to be able to customize the UserAgent for an embedded UIWebView. (For instance, I'd like the server to respond differently if, say, a user is using one version of the app versus another.)

是否可以像在 Windows 应用程序中的嵌入式 IE 浏览器一样自定义现有 UIWebView 控件中的 UserAgent?

Is it possible to customize the UserAgent in the existing UIWebView control the way it is, say, for an embedded IE browser in a Windows app?

推荐答案

Modern Swift

以下是 StackOverflow 用户 PassKit 和 Kheldar 对 Swift 3+ 项目的建议:

Modern Swift

Here's a suggestion for Swift 3+ projects from StackOverflow users PassKit and Kheldar:

UserDefaults.standard.register(defaults: ["UserAgent" : "Custom Agent"])

来源:https://stackoverflow.com/a/27330998/128579

随着 iOS 5 的变化,我推荐以下方法,最初来自这个 StackOverflow 问题:UIWebView iOS5正如下面的答案所指出的那样,正在更改用户代理.在该页面的评论中,它似乎也适用于 4.3 及更早版本.

With iOS 5 changes, I recommend the following approach, originally from this StackOverflow question: UIWebView iOS5 changing user-agent as pointed out in an answer below. In comments on that page, it appears to work in 4.3 and earlier also.

通过运行此代码一次更改UserAgent"默认值您的应用启动:

Change the "UserAgent" default value by running this code once when your app starts:

NSDictionary *dictionary = @{@"UserAgent": @"Your user agent"};
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
[[NSUserDefaults standardUserDefaults] synchronize];

如果您需要在 4.3/5.0 之前的 iOS 版本中运行的方法,请参阅此帖子的先前编辑.请注意,由于进行了大量编辑,此页面上的以下评论/其他答案可能没有意义.毕竟,这是一个四年前的问题.;-)

See previous edits on this post if you need methods that work in versions of iOS before 4.3/5.0. Note that because of the extensive edits, the following comments / other answers on this page may not make sense. This is a four year old question, after all. ;-)

这篇关于在 UIWebView 中更改用户代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆