Change agent-agent

How can I change the default User-Agent string in my WebView?

@IBOutlet weak var myWbView: UIWebView!
let myURL = NSURL(string: "http://http://web-example")
let myURLRequest:NSURLRequest = NSURLRequest(URL: myURL!)
myWbView.loadRequest(myURLRequest)

      

+1


source to share


2 answers


If you want to set the User-Agent HTTP header for your request to be used to load into the webview,

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 application see this question How to set "User-Agent" header of UIWebView in Swift

+2


source


Actually, it's very easy. For this you have to use NSMutableURLRequest

, initialize it with NSURL

and set any value of the user agent using the method setValue:ForHTTPHeaderField:

where the field will be User-Agent

, load it into the web view. It! Good luck!



0


source







All Articles