Change User Agent in UIWebView

ios wkwebview set user agent
swift wkwebview change user-agent
ios webview user agent
user-agent swift
react native change user-agent
ios webkit user agent
ios 13 wkwebview user agent
swift get user agent

I have a business need to be able to customize the UserAgent for an embedded UIWebView. (For instance, I'd like the server to respond differently if, say, a user is using one version of the app versus another.)

Is it possible to customize the UserAgent in the existing UIWebView control the way it is, say, for an embedded IE browser in a Windows app?

Modern Swift

Here's a suggestion for Swift 3+ projects from StackOverflow users PassKit and Kheldar:

UserDefaults.standard.register(defaults: ["UserAgent" : "Custom Agent"])


Earlier Objective-C Answer

With iOS 5 changes, I recommend the following approach, originally from this StackOverflow question: UIWebView iOS5 changing user-agent as pointed out in an answer below. In comments on that page, it appears to work in 4.3 and earlier also.

Change the "UserAgent" default value by running this code once when your app starts:

NSDictionary *dictionary = @{@"UserAgent": @"Your user agent"};
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
[[NSUserDefaults standardUserDefaults] synchronize];

See previous edits on this post if you need methods that work in versions of iOS before 4.3/5.0. Note that because of the extensive edits, the following comments / other answers on this page may not make sense. This is a four year old question, after all. ;-)

Change UIWebView User-Agent. � GitHub, Change UIWebView User-Agent. gistfile1.m. - (BOOL)webView:(UIWebView *) webView shouldStartLoadWithRequest:(NSURLRequest *)req� You can change this user agent to other user agents you like. So we set the string, now let’s call it. webview = (WebView) findViewById (; webview.getSettings ().setUserAgentString (MyUA); Here we called the string that we have set, it is ready now.

I had this problem too, and tried all methods. I found that only this method works (iOS 5.x): UIWebView iOS5 changing user-agent

The principle is to set the user agent permanently in the user settings. This works; Webview sends the given header. Just two lines of code:

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"Mozilla/Whatever version 913.6.beta", @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];

Setting User-Agent, or User_Agent in the mutable request, or overriding the setValue in the NSHttpRequest by swizzling, - I tried all that and controlled the results with wireshark, and none of that seems to work, because Webview still uses the user agent value from the user defaults, no matter what you try to set in the NSHttpRequest.

Customize the user agent in WebView (iOS) — Repro documentation, to customize your user agent. Please perform the customization once only during the app launch and before any instantiation of UIWebView or WKWebView. So let’s change it: public static final String USER_AGENT = " Custom UserAgent here "; webSettings.setUserAgentString(USER_AGENT); Here is the good news: If you use the WebViewGold Android Studio template, you do not need to worry about the whole custom UserAgent mechanisms, just enter the customized UserAgent in the configuration file:

It should work with an NSMutableURLRequest as Kuso has written.

NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @""]];
[urlRequest setValue: @"iPhone" forHTTPHeaderField: @"User-Agent"]; // Or any other User-Agent value.

You'll have to use NSURLConnection to get the responseData. Set the responseData to your UIWebView and the webView should render:

[webView loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)encodingName baseURL:(NSURL *)baseURL];

Spoof the User Agent for a UIWebView? : iOSProgramming, Hi All - I'm trying to change the user agent of a UIWebView in a simple proof of concept app, but I'm not able to make it work. here is my code … UIWebView is now deprecated and WKWebView should now be used in order to upload an app to the AppStore. Original App Store Connect message after uploading the App: ITMS-90809: Deprecated API Usage - Apple will stop accepting submissions of apps that use UIWebView APIs .

Very simple in Swift. Just place the following into your App Delegate.

UserDefaults.standard.register(defaults: ["UserAgent" : "Custom Agent"])

If you want to append to the existing agent string then:

let userAgent = UIWebView().stringByEvaluatingJavaScript(from: "navigator.userAgent")! + " Custom Agent"
UserDefaults.standard.register(defaults: ["UserAgent" : userAgent])

Note: You may will need to uninstall and reinstall the App to avoid appending to the existing agent string.

Radar: Allow Overriding of User-Agent on UIWebView, Suggested Solution Expose a userAgent property on UIWebView that uses the default user-agent when nil or otherwise sends the set string. In iOS, it's possible to set a custom user agent for a UIWebView like this: NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:agent, @"UserAgent", nil]; [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary]; In OSX, there was a setCustomUserAgent method for WebView elements that did the trick.

Actually adding any header field to the NSURLRequest argument in shouldStartLoadWithRequest seems to work, because the request responds to setValue:ForHTTPHeaderField - but it doesn't actually work - the request is sent out without the header.

So I used this workaround in shouldStartLoadWithRequest which just copies the given request to a new mutable request, and re-loads it. This does in fact modify the header which is sent out.

if ( [request valueForHTTPHeaderField:@"MyUserAgent"] == nil )
    NSMutableURLRequest *modRequest = [request mutableCopyWithZone:NULL];
    [modRequest setValue:@"myagent" forHTTPHeaderField:@"MyUserAgent"];
    [webViewArgument loadRequest:modRequest];
    return NO;

Unfortunately, this still doesn't allow overriding the user-agent http header, which is apparently overwritten by Apple. I guess for overriding it you would have to manage a NSURLConnection by yourself.

Sending a custom string along with the existing user agent in , I am trying to send a custom string along with the existing user agent in UIWebView, Can you verify and please correct this code ? Custom User Agent. To set a custom User Agent you can use customUserAgent property: let webConfiguration = WKWebViewConfiguration() let webView = WKWebView(frame: .zero, configuration: webConfiguration) webView.customUserAgent = "ExampleApp/1.0 (iPhone)" Available: iOS 9+.

Correctly Form User-Agents for Webview Apps, See github for full code examples. Objective C. In Objective C, we retrieve the user-agent by creating a webview: UIWebView* webView� Allow overriding of user-agent on UIWebView Problem For some use cases it is necessary to modify the user agent that UIWebView sends towards the server to get a different version of the content. Such scenarios where we needed that included opening a special version of an online shopping bag or opening the full-version of Facebook instead of the

The perils of iOS user agent strings, a user agent for Safari and a user agent for the UIWebView. This assumption is incorrect as iOS apps can and do customize their user agent. I tried to change User-Agent using NSMutableURLRequest in the UIWebView delegate function, but didn't work. The funny thing isthe User-Agent string changed if the request is redirect. Have anyone experienced it? I want to change User-Agent of every request from UIWebView to the same like coming from Safari browser. Please HELP!!

Webviews and User-Agent strings, Changing the User-Agent string of a webview is simple. In an Android app for example, you would change it with a line of code something like� Changing the user agent is actually considered a developer option that helps webmasters check their pages against potential compatibility issues with different browsers. In the Developer tools

  • One downside to this approach: If your webView contains custom urls (someapplication://) they will fail since you're always returning YES.
  • @stigi try to change the key from @"User_Agent" to @"User-Agent". That worked for me.
  • Anyone knows how to persist it through AJAX calls?
  • @nmr This was back in 3.x days. See my "2012 update" at the top of the post. And no, sorry, I can't say why it was necessary back then. It's possible that another layer in the TCP stack converts headers with underscores into headers with hyphens. All I can say is I think they've since fixed this behaviour, so I wouldn't worry about it.
  • Could this be done initially in the controller for the web view? Like checking the url first and based on that call this [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary]; ? Basically I already tried but did not work out. Just want to know if it is even possible since you suggest to do this initially on startup.
  • Only this worked for me! you can now use a shorter version: [[NSUserDefaults standardUserDefaults] registerDefaults:@{ @"UserAgent": @"custom agent" }];
  • what would user agent string for safari on mac look like?
  • Thanks. Saved lot of time!
  • Thanks @Kheldar for updating for Swift 3. For earlier Swift versions check the edit history.
  • tested in iOS7 on iPhone 5 device and it works, doesn't work on simulator but that doesnt' matter. thanks for the info.
  • startSpoofingUserAgent works for me however unregisterDefaultForKey is not a valid command. the only way I got this to work was to do a registerDefaults with the original value to reset it. -rrh
  • unregisterDefaultForKey: is implemented here: (as noted in the answer).
  • Since iOS 5 the iOS no longer uses the public API to create its NSURLRequest objects anymore, so the swizzling method won’t work anymore under iOS 5.