Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change User-Agent

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-example")
let myURLRequest:NSURLRequest = NSURLRequest(URL: myURL!)
myWbView.loadRequest(myURLRequest)
like image 200
Phocs Avatar asked Apr 27 '16 12:04

Phocs


People also ask

What does changing user agent do?

Changing User-Agent allows you to mimic, spoof or fake other browsers, devices or search engine spiders. The extension does not render web pages in the same way as the selected one. User-Agent Switcher is primarily for developers who want to test how a page responds to different browsers.

How do I permanently change user agent?

Select the “Network“. Select the “Menu” icon located at the upper-right corner, then choose “More tools” > “Network conditions“. Uncheck the “Select automatically” check box, then choose the user agent you wish to use. in the drop-down menu.

How do I change the user agent on my iPhone?

It's not possible to change your user agent string on iPhone. The user agent string you provided is the standard string that an iPhone uses; nothing out of the ordinary there.

How do I change user agent in developer tools?

To open them, click the settings menu and select “F12 Developer Tools” or just press F12 on your keyboard. The developer tools will open in a separate pane at the bottom of the window. Click the “Emulation” tab and choose a user agent from the “User agent string” box.


1 Answers

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

like image 57
Alexander Tkachenko Avatar answered Sep 18 '22 12:09

Alexander Tkachenko