如何在 OS X Yosemite 中更改 WKWebview 的用户代理? [英] How do I change the WKWebview's user agent in OS X Yosemite?

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

问题描述

如何更改WKWebview 使用的用户代理?

How do you change the user agent used by WKWebview?

使用旧的 WebView,我可以编写以下内容来更改用户代理:

With the older WebView, I could write the following to change the user agent:

[myWebView setCustomUserAgent:@"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4)
 AppleWebKit/537.77.4 (KHTML,like Gecko) Version/7.0.5 Safari/537.77.4"];

推荐答案

在 Swift 中非常简单.只需将以下内容放入您的 App DelegatedidFinishLaunchingWithOptions.

Very simple in Swift. Just place the following into your App DelegatedidFinishLaunchingWithOptions.

NSUserDefaults.standardUserDefaults().registerDefaults(["UserAgent" : "Custom Agent"])

如果您想附加到现有的代理字符串,则:

If you want to append to the existing agent string then:

let userAgent = UIWebView().stringByEvaluatingJavaScriptFromString("navigator.userAgent")! + " Custom Agent"
NSUserDefaults.standardUserDefaults().registerDefaults(["UserAgent" : userAgent])

注意:您需要卸载并重新安装应用程序以避免附加到现有代理字符串.

Note: You will need to uninstall and reinstall the App to avoid appending to the existing agent string.

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

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