UIWebView User-Agent未正确设置为自定义值

时间:2023-01-12 21:43:28

If we want to change the value of user agent from

如果我们想要更改用户代理的值

Mozilla/5.0 (iPhone; CPU iPhone OS 10_1 like Mac OS X) AppleWebKit/602.2.14 (KHTML, like Gecko) Mobile/14B72

to

Mozilla/5.0 (iPad; CPU OS 10_1 like Mac OS X) AppleWebKit/602.2.14 (KHTML, like Gecko) Mobile/14B72

i.e. On iPhone app, my app needs to set the User-Agent of iPad.

即在iPhone应用程序上,我的应用程序需要设置iPad的User-Agent。

My code looks like

我的代码看起来像

class ViewController: UIViewController, UIWebViewDelegate {
    @IBOutlet weak var webView: UIWebView!
    var userAgent:String = ""
    override func viewDidLoad() {
        super.viewDidLoad()
        userAgent = self.webView.stringByEvaluatingJavaScript(from: "navigator.userAgent")!

        let range = userAgent.range(of: "iPhone")

        if range != nil {
            userAgent = userAgent.replacingCharacters(in: range!, with: "iPad")
            userAgent = userAgent.replacingOccurrences(of: "iPhone", with: "")
        }
        userAgent = userAgent.replacingOccurrences(of: "  ", with: " ")

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

    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        self.webView.loadRequest(URLRequest(url: URL(string: "http://www.google.com")!))
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        let userAgent = request.allHTTPHeaderFields!["User-Agent"]
        print("\(userAgent)")
        return true
    }

}

In above case the User-Agent is not set and keeps default value

在上述情况下,未设置User-Agent并保持默认值

Instead of logic to manipulate the string to look like iPad's User-Agent, If I change and set hard coded value in viewDidLoad like

而不是逻辑来操纵字符串看起来像iPad的用户代理,如果我在viewDidLoad中更改并设置硬编码值

UserDefaults.standard.register(defaults: ["UserAgent": “Mozilla/5.0 (iPad; CPU OS 10_1 like Mac OS X) AppleWebKit/602.2.14 (KHTML, like Gecko) Mobile/14B72”])

then also it is not updated.

然后它也没有更新。

But if I hard code like

但如果我硬编码就好

UserDefaults.standard.register(defaults: ["UserAgent": “MyValue”])

then it is properly set

然后它被正确设置

Any idea, what went wrong here?

有什么想法,这里出了什么问题?

2 个解决方案

#1


1  

If you call to get iOS browser user agent value with js code, iOS will creat and send value at this time. So you can not update user agent after create. Try to write all user agents as manually without calling by javascript like this:

如果您使用js代码调用iOS浏览器用户代理值,iOS将在此时创建并发送值。因此,您无法在创建后更新用户代理。尝试手动编写所有用户代理,而无需通过javascript调用,如下所示:

    let manuelUserAgent = "Mozilla/5.0 (iPad; CPU OS 10_1 like Mac OS X) AppleWebKit/602.2.14 (KHTML, like Gecko) Mobile/14B72"

    let newUserAgent = "\(manuelUserAgent) YourOpt.Text"

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

#2


0  

There is an interesting workout for this that it helped me in a similar project:

有一个有趣的锻炼,它帮助我在一个类似的项目:

  • It is true that once you get the value of a http header you can not modify it later on (is it a bug?)
  • 确实,一旦你获得了http标头的值,你就不能在以后修改它(这是一个错误吗?)

  • because of that you cant obtain the current value and modify it
  • 因为你不能获得当前值并修改它

  • That is true for one instance of a webview object. That is why you can add a temporal object with the "hidden" property with true value, so it wont appear in your GUI and then obtain the value from this object so you can modify it on the original object:

    对于webview对象的一个​​实例也是如此。这就是为什么你可以添加一个带有true值的“hidden”属性的临时对象,因此它不会出现在你的GUI中,然后从这个对象中获取值,这样你就可以在原始对象上修改它:

    @IBOutlet var tmpWebView: UIWebView!

    @IBOutlet var tmpWebView:UIWebView!

    @IBOutlet var webview: UIWebView!

    @IBOutlet var webview:UIWebView!

    var newAgent = tmpWebView.stringByEvaluatingJavaScript(from: "navigator.userAgent")! + " Added new string"
    UserDefaults.standard.register(defaults: ["UserAgent": newAgent])
    

#1


1  

If you call to get iOS browser user agent value with js code, iOS will creat and send value at this time. So you can not update user agent after create. Try to write all user agents as manually without calling by javascript like this:

如果您使用js代码调用iOS浏览器用户代理值,iOS将在此时创建并发送值。因此,您无法在创建后更新用户代理。尝试手动编写所有用户代理,而无需通过javascript调用,如下所示:

    let manuelUserAgent = "Mozilla/5.0 (iPad; CPU OS 10_1 like Mac OS X) AppleWebKit/602.2.14 (KHTML, like Gecko) Mobile/14B72"

    let newUserAgent = "\(manuelUserAgent) YourOpt.Text"

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

#2


0  

There is an interesting workout for this that it helped me in a similar project:

有一个有趣的锻炼,它帮助我在一个类似的项目:

  • It is true that once you get the value of a http header you can not modify it later on (is it a bug?)
  • 确实,一旦你获得了http标头的值,你就不能在以后修改它(这是一个错误吗?)

  • because of that you cant obtain the current value and modify it
  • 因为你不能获得当前值并修改它

  • That is true for one instance of a webview object. That is why you can add a temporal object with the "hidden" property with true value, so it wont appear in your GUI and then obtain the value from this object so you can modify it on the original object:

    对于webview对象的一个​​实例也是如此。这就是为什么你可以添加一个带有true值的“hidden”属性的临时对象,因此它不会出现在你的GUI中,然后从这个对象中获取值,这样你就可以在原始对象上修改它:

    @IBOutlet var tmpWebView: UIWebView!

    @IBOutlet var tmpWebView:UIWebView!

    @IBOutlet var webview: UIWebView!

    @IBOutlet var webview:UIWebView!

    var newAgent = tmpWebView.stringByEvaluatingJavaScript(from: "navigator.userAgent")! + " Added new string"
    UserDefaults.standard.register(defaults: ["UserAgent": newAgent])