let url = NSURL(string:"http://www.examplepage.com/“)
let Request = NSMutableURLRequest(URL: url!)
Request.setValue("Custom-Agent", forHTTPHeaderField: "User-Agent")
let task1 = NSURLSession.sharedSession().dataTaskWithRequest(Request){ data, response, error in
let contentPage = NSString(data: data!, encoding: NSUTF8StringEncoding)
self.webView!.loadHTMLString(contentPage! as String, baseURL: url)
}
task1.resume()
I’ve used the following code to set the “User Agent” header of a WKWebView.
It works well when the app first loaded, but only after a button is clicked into the link the “User-Agent” changes.
How can I prevent the "User-Agent" from changing?
For iOS 9, use the customUserAgent property on the web view.
For iOS 8 and earlier, do this instead:
NSUserDefaults.standardUserDefaults().registerDefaults(["UserAgent" : "BlahBlahBlah"])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With