在UIWebView中更改用户代理 [英] Change User Agent in 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.)
是否可以自定义
推荐答案
Modern,是否可以使用现有UIWebView中的UserAgent来控制Windows应用中的嵌入式IE浏览器? 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屋!