// 方法一强写UserAgent 为ios,JS大神要的也是醉了.原生数据更加详细.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// 直接强行写入了UserAgent 的值为 ios
let ios : String = "ios"
UserDefaults.standard.register(defaults: ["UserAgent" : ios])
UserDefaults.standard.synchronize()
print(UserDefaults.standard.object(forKey: "UserAgent")!)
return true
}
//方法二在加载webView的时候通过JS方法拿出默认navigator.userAgent,然后添加和网络商定的判定字符.然后更新注册信息.存回去.
// MARK:加载webView开始的时候设置userAgent
func webViewDidStartLoad(_ webView: UIWebView) {
let userAgent = webView.stringByEvaluatingJavaScript(from: "navigator.userAgent")
let webViewAndString = "\(userAgent)+想加的东西"
UserDefaults.standard.register(defaults: ["UserAgent" : webViewAndString])
UserDefaults.standard.synchronize()
print(UserDefaults.standard.object(forKey: "UserAgent")!)
}
但我不建议这么做,意义不大底层写的更详细.打印后如下:
Mozilla/5.0 (iPhone; CPU iPhone OS 10_1_1 like Mac OS X) AppleWebKit/602.2.14 (KHTML, like Gecko) Mobile/14B100
网友评论