Our Blog
This blog is mainly dedicated to the iPhone and iPad programming as well as informations about our applications
This blog is mainly dedicated to the iPhone and iPad programming as well as informations about our applications

UIWebView provides no way to change the User-Agent.
And not only it doesn't provide anything to change it, but it also override this value when we try to set the User-Agent in a NSMutableURLRequest object passed as a request in the loadRequest UIWebView method.
There are some solutions i found on the web that works but none are perfect :
One solution is to set User_Agent instead User-Agent in a NSMutableURLRequest.
That works for many websites but not all because setting User_Agent doesn't override User-Agent, that just adds another header value called User_Agent that some website uses instead of User-Agent.
Another solution that works well is to use method swizzling in the NSMutableURLRequest class to watch when the User-Agent is set and override the value to have the desired User-Agent.
The problem with this method is that is not very easy to implement and method swizzling can be dangerous if we are not careful with this.
The other problem is that Apple can reject your application if it uses method swizzling.
So i found (with luck) a solution which is simple, without using method swizzling and which overrides the true User-Agent (not User_Agent).
Here is the class method to add to your main controller (or app delegate) :
great!
it works perfectly!
I was struggling to find a good way to do this for android as well. This is what I've found
Really simple way is to fetch the...
I think this board is the proper place to ask you about the activation proccess. My link is not working properly, do you know why it is...
You can put what you want in the desired user agent.
Yes it's "UserAgent", there is no typo, but don't forget that you need to set...
Can you give us a sample of what belongs in "Desired User Agent?" I'm using:
Mozilla/5.0 (iPhone Simulator; U; CPU iPhone OS 4_2...
10 comments on "Easily set the User-Agent in a UIWebView"
Casey (not verified) - Dynamic UserAgent
Thanks for this awesome post. This is seemingly the only method that currently works without using undocumented methods.
I have this code working, however when I call the function again, to change the UserAgent to something else, UIWebView's do not update. In order to successfully update the UserAgent I need to remove/release the UIWebView and alloc/show it again.
-(void)setUserAgent:(NSString*)userAgent {NSDictionary *userAgentReplacement = [[NSDictionary alloc] initWithObjectsAndKeys:userAgent, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:userAgentReplacement];
//SHOULDN'T HAVE TO DO THIS
//get webView values then release
CGRect webFrame = webView.frame;
NSURLRequest *webRequest = [[webView request] retain];
[webView removeFromSuperview];
[webView release];
//allow webview and add to screen again
webView = [[UIWebView alloc] initWithFrame:webFrame];
[webView setScalesPageToFit:TRUE];
[webView setDelegate:self];
[self.view addSubview:webView];
}
This is mostly OK however it causes the UIWebView to lose all history. Is there any way to update the UserAgent without having to create a new UIWebView in order to have it take effect?
Thanks again
Anonyme - Interesting information about activation
I think this board is the proper place to ask you about the activation proccess. My link is not working properly, do you know why it is happening? http://www.mphweb.com/?15667bdbef97fc92aa0b25ff816,
Kieran (not verified) - Fantastic. This works a treat
Fantastic. This works a treat and only 5 lines of code. I was stuck on this problem for ages. Thanks a lot
spstanley (not verified) - Awesome solution, thank you
Awesome solution, thank you very much!
Eimantas (not verified) - Tip
You MUST use
registerDefaultsassetValue:forKeywill not work!inator (not verified) - use with phonegap childbrowser
I'd like to apply this fix to the childbrowser plugin in phonegap. Anyone know how I'd do this? Having no real useful experience with obj-c, I do't know where to begin.
Adam Bossy (not verified) - Can you give us a sample of
Can you give us a sample of what belongs in "Desired User Agent?" I'm using:
Mozilla/5.0 (iPhone Simulator; U; CPU iPhone OS 4_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Mobile/8C134 Safari/528.16
... to no avail.
Also, I assume "UserAgent" is meant to be un-hypenated and is not a typo?
mphweb - You can put what you want in
You can put what you want in the desired user agent.
Yes it's "UserAgent", there is no typo, but don't forget that you need to set the value very early in the app (it's why i put it in + (void)initialize)
optedoblivion (not verified) - User Agent
I was struggling to find a good way to do this for android as well. This is what I've found
Really simple way is to fetch the android version from github. browse to framework/src/com/phonegap and edit DroidGap.java
scroll down to find the line
settings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL);
Then after that line add this
settings.setUserAgentString("HerpDerp Agent");
Jaye Jung (not verified) - great! it works perfectly!
great!
it works perfectly!
Post new comment