Change user agent in UIWebView

I have a business to be able to configure UserAgent for the embedded UIWebView. (For example, I would like the server to react differently if, say, the user uses one version of the application compared to another.)

Is it possible to configure UserAgent in an existing UIWebView control, as, say, for the built-in IE browser in a Windows application?

+66
ios iphone cocoa-touch uiwebview
Jan 25 '09 at 21:57
source share
13 answers

Modern Swift

Here is the offer for Swift 3+ projects from StackOverflow PassKit and Kheldar users:

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

Source: stack overflow

Formerly Objective-C Response

With changes to iOS 5, I recommend the following approach, initially from this StackOverflow question: UIWebView iOS5 modifies the user-agent as mentioned in the answer below. The comments on this page seem to work in 4.3 and earlier as well.

Change the default value of "UserAgent" by running this code once at application startup:

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

See previous edits in this post if you need methods that work in iOS versions prior to 4.3 / 5.0. Please note that due to extensive edits, the following comments / other answers on this page may not make sense. After all, this is a four-year question. ;-)

+89
Sep 02 '09 at 15:28
source share

It should work with NSMutableURLRequest, as Kuso wrote.

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

You will need to use NSURLConnection to get the response information. Set responseData to your UIWebView, and the webView should display:

 [webView loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)encodingName baseURL:(NSURL *)baseURL]; 
+18
Feb 17 '09 at 21:16
source share

I had this problem too, and I tried all the methods. I found that only this method works (iOS 5.x): User Agent UIWebView iOS5

The principle is to constantly install the user agent in user settings. It works; Webview sends this 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 a mutable request or overriding setValue in NSHttpRequest by swizzling - I tried all this and controlled the results using wirehark, and none of this works because Webview still uses the user agent value from user defaults , regardless of what you are trying to install in NSHttpRequest.

+18
Jun 19 '12 at 8:01
source share

Very simple in Swift. Just put the following in your application.

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

If you want to add an operator line to an existing line, then:

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

Note. You may need to uninstall and reinstall the application to avoid adding an agent to an existing line.

+10
Dec 06 '14 at 11:10
source share

Taking everything as it was decided for me:

 - (void)viewDidLoad { NSURL *url = [NSURL URLWithString: @"http://www.amazon.com"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setValue:@"Foobar/1.0" forHTTPHeaderField:@"User-Agent"]; [webView loadRequest:request]; } 

Thanks to everyone.

+6
Jan 05 '10 at
source share

Using @ "User_Agent" simply results in a custom header in the GET request.

User_agent: Foobar / 1.0 \ r \ n - User-Agent: Mozilla / 5.0 (iPhone; U; CPU iPhone OS 3_1_2, like Mac OS X; en-us) AppleWebKit / 528.18 (KHTML, e.g. Gecko) Mobile / 7D11 \ r \ n

The above is what appears in the split HTTP package, which essentially confirms what Sfjava quoted on this forum. It is interesting to note that the "User-Agent" turns into a "User_agent".

+6
Jan 12
source share

Actually, adding any header field to the NSURLRequest argument in the shouldStartLoadWithRequest file seems like the request responds to setValue: ForHTTPHeaderField - but it does not work . The request is sent without a header.

So, I used this workaround in the shouldStartLoadWithRequest file, which simply copies the given request into a new mutable request and reloads it. This actually changes the header that is being sent.

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

Unfortunately, this still does not allow overriding the HTTP header of the user-agent, which is apparently being rewritten by Apple. I think for redefinition you should manage NSURLConnection independently.

+6
Sep 21 '10 at 13:57 on
source share

This decision seems to have been seen as a pretty smart way to do it.

changing-the-headers-for-uiwebkit-http-requests

It uses the Swizzling method, and you can learn more about it on the CocoaDev page

Look at this!

+4
Aug 05 '10 at 16:13
source share

By combining Louis Saint-Amour 's NSUserDefaults+UnRegisterDefaults and NSUserDefaults+UnRegisterDefaults category from this question / answer , you can use the following methods to start and stop spoofing user-agent at any time while your application is running:

 #define kUserAgentKey @"UserAgent" - (void)startSpoofingUserAgent:(NSString *)userAgent { [[NSUserDefaults standardUserDefaults] registerDefaults:@{ kUserAgentKey : userAgent }]; } - (void)stopSpoofingUserAgent { [[NSUserDefaults standardUserDefaults] unregisterDefaultForKey:kUserAgentKey]; } 
+4
Aug 07 '13 at
source share

I had the same question. I want to add some information to the user agent, it is also necessary to keep the original webview user agent. I solved this using the following code:

  //get the original user-agent of webview UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero]; NSString *oldAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"]; NSLog(@"old agent :%@", oldAgent); //add my info to the new agent NSString *newAgent = [oldAgent stringByAppendingString:@" Jiecao/2.4.7 ch_appstore"]; NSLog(@"new agent :%@", newAgent); //regist the new agent NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:newAgent, @"UserAgent", nil]; [[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary]; 

Use it before creating a web view.

+2
May 14 '14 at 12:10
source share

Try this at AppDelegate.m

 + (void)initialize { // Set user agent (the only problem is that we can't modify the User-Agent later in the program) // iOS 5.1 NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:@"Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B176 Safari/7534.48.3", @"UserAgent", nil]; [[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary]; } 
+1
Jan 17 '13 at 12:28
source share

The only problem I encountered is changing only the user agent

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys: @"Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5", @"UserAgent", nil]; [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary]; } 
+1
Feb 08 '13 at 12:10
source share

To add custom content to the current UserAgent value, do the following:

1 - Get user agent value from NEW WEBVIEW

2 - add custom content to it

3 - Save the new value in the dictionary using the UserAgent key

4 - Save the dictionary in the standard UserDefaults.

See an example below:

 NSString *userAgentP1 = [[[UIWebView alloc] init] stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"]; NSString *userAgentP2 = @"My_custom_value"; NSString *userAgent = [NSString stringWithFormat:@"%@ %@", userAgentP1, userAgentP2]; NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:userAgent, @"UserAgent", nil]; [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary]; 
0
Sep 11 '15 at 19:02
source share



All Articles