How can I change in my WebView the default string of User-Agent?
@IBOutlet weak var myWbView: UIWebView!
let myURL = NSURL(string: \"http://http://web-exampl
Swift 4.2
You always can override global UserAgent field, it is resolve your problem:
UserDefaults.standard.register(defaults: ["UserAgent" : "Custom UserAgent)"])
If you want to set User-Agent HTTP header for your request that is going to be used for Web-view loading,
let userAgent = "Custom User Agent";
let myURL = NSURL(string: "http://http://web-example")
let myURLRequest:NSURLRequest = NSMutableURLRequest(URL: myURL!)
myWbView.loadRequest(myURLRequest)
myURLRequest.setValue(userAgent, forHTTPHeaderField: "User-Agent")
If you want to set User-Agent for all requests in your app, see this question How can I set the "User-Agent" header of a UIWebView in Swift
Actually, this is very easy. For that, you should use NSMutableURLRequest
, initialize it with the NSURL
, and set any user agent value using method setValue:ForHTTPHeaderField:
, where field would be User-Agent
, load it on the web view. That's it! Good luck!