Change User Agent in Uiwebview

How can I set the User-Agent header of a UIWebView in Swift

This will change the agent of any call made through the 'normal' stack.

Swift 2:

NSUserDefaults.standardUserDefaults().registerDefaults(["UserAgent": "Custom-Agent"])

Swift 3:

UserDefaults.standard.register(defaults: ["UserAgent": "custom value"])

Can't change UserAgent in UIWebview after loading page

The UserAgent user-defaults key is private API. It therefore has no guaranteed behaviour. All answers will be empirical.

That being said, it sounds like the correct solution is to kill and recreate your UIWebView following each change. UIView doesn't conform to NSCopying so you'll need to do something manual such as:

UIWebView *newWebView = [[UIWebView alloc] initWithFrame:self.webView.frame];
newWebView.delegate = self;
newWebView.scalesPageToFit = YES;
// ... and the autoreseizing mask, etc — no need to do this upon every load

[self.webView.superview insertSubview:newWebView aboveSubview:self.webView];
[self.webView removeFromSuperview];
self.webView = newWebView;

If that fails to work, you probably need to switch to a documented means of setting the user agent. I'd suggest implementing your own NSURLProtocol handler that accepts http and https requests it hasn't yet seen, modifies the header field and dispatches them a second time, that time declining to take them so that they drop down to the system. It'll be hassle but it'll allow you to change that, or any other header field, reliably and with immediate effect.

See kifi's engineering blog for an example.

How to get UIWebView User-Agent

I just ran into a similar issue and needed to make the user agent sent by an NSURLConnection match the one sent by a UIWebView. My solution was to create a UIWebView and then just use javascript to pull out the user agent.

UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectZero];
NSString* secretAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];

Change User-Agent

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

Set useragent in WKWebview

You'll be happy to hear that WKWebView just gained a customUserAgent property in iOS 9 and OSX 10.11

Example:

wkWebView.customUserAgent = "your agent" 

UIWebView Navigator userAgent property changing with iOS9 doesn't work

The user agent needs to be set in the UserDefaults.
You can use the following code to set it up.

func setUserAgent(as userAgentString: String) {
UserDefaults.standard.register(defaults: ["UserAgent": userAgentString])
}

Usage:

let userAgentString = "USER AGENT STRING HERE"
setUserAgent(as: userAgentString)

EDIT:

I see that you want to append some string to the current UserAgent.
For that you need to read the value for the current user agent, store it, add your custom string, then store the new user agent. Usage stays the same.

func setUserAgent(as value: String) {

let webView = UIWebView(frame: CGRect(width: 0, height: 0))
let defaultUserAgentString = webView.stringByEvaluatingJavaScript(from: "navigator.userAgent")

UserDefaults.standard.register(defaults: ["UserAgent": defaultUserAgentString + " " + value])
// remove the " " space if you don't need it
}

EDIT 2:

Yes, changing it will change it for all the web views across your app. To avoid this you can...

func setUserAgent(as value: String) {

let webView = UIWebView()
Constants.defaultUserAgentString = webView.stringByEvaluatingJavaScript(from: "navigator.userAgent") ?? ""

UserDefaults.standard.register(defaults: ["UserAgent": defaultUserAgentString + " " + value])
}

func resetUserAgent() {

if let defaultString = Constants.defaultUserAgentString {
UserDefaults.standard.register(defaults: ["UserAgent": defaultString])
}
}

Constants here is a struct/class that is used to store global constants, you may replace it with your own struct/class if you have one. Just add static variable defaultUserAgentString to keep track of the default user agent.
Use resetUserAgent() to switch back to the default user agent when you done. Maybe add it to viewWillDisappear() on the ViewController handling the WebView that needs the modified user agent.



Related Topics



Leave a reply



Submit