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.
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
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
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?
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: