11

My Phonegap application is allowed to browse to some external sites that also use the Phonegap API. Currently I am conditionally including the Phonegap javascript based on what platform you are on (Android, iOS, etc). However, I can't tell the difference between Phonegap applications and the regular browser on a mobile device. Is there a way to change the user agent in my Phonegap app to give my server a hint about this?

Most interested in the iOS solution to this.

Nick Retallack
  • 18,986
  • 17
  • 92
  • 114

6 Answers6

18

If you're still looking for a quick solution for that one, this is how I achieved it:

// Modify the user-agent
NSString* suffixUA = @" my nice user-agent suffix";
UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectZero]; 
NSString* defaultUA = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
NSString* finalUA = [defaultUA stringByAppendingString:suffixUA];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:finalUA, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];

In the Phonegap (Cordova) sample app, you need to add these lines in the didFinishLaunchingWithOptions method of AppDelegate.m

I spent an entire day trying to figure this one out! I'm sure it could be useful to other people.

Alanou
  • 181
  • 3
  • 2
    Would be nice to include the android solution too. – Nick Retallack Oct 10 '12 at 01:35
  • I found the Android solution at http://stackoverflow.com/questions/14406393/change-phonegap-cordova-user-agent-for-ajax – Krishna Feb 18 '13 at 19:52
  • 4
    To get @alanou solution working you should add the code he posted at the end of the didFinishLaunchingWithOptions method just before the return statement. It seems that [self.window makeKeyAndVisible]; must run before you can change the UA. Test OK on iPhone 4 with iOS 6.1.3 – redochka Apr 25 '13 at 09:31
7

This answer to a similar question also mentions a preference available in current Cordova versions:

<preference name="OverrideUserAgent" value="MyCordovaApp/1.2.3" />

It is documented for Android and iOS.

Community
  • 1
  • 1
Gregor Schmidt
  • 639
  • 4
  • 11
6

CB-2520 was resolved, which proposed allowing the user-agent string to be modified. You can achieve this now by setting the baseUserAgent property of the MainViewController. For a simple test, you can add the following to the didFinishLaunchingWithOptions method of the AppDelegate.m, after the MainViewController is initialized:

self.viewController.baseUserAgent = @"My Custom User Agent";

Mike MacMillan
  • 1,575
  • 1
  • 11
  • 7
3

If you're looking for a more powerful http client for cordova that allows changing the user agent, or any header in that matter, try out https://github.com/aporat/cordova-plugin-fetch. it wraps around well tested native networking libraries (AFNetworking 2 on ios, and OKHttp on android).

it also follows the window.fetch, so you could use cordovaFetch on the simulators and devices, while testing on the browser with fetch.js.

Just install the plugin with

cordova plugin add https://github.com/aporat/cordova-plugin-fetch.git

and include the user agent header in any request you made.

cordovaFetch('/users.json', {
  method : 'GET',
  headers: {
   'User-Agent': 'your user agent'
 },
})
aporat
  • 5,922
  • 5
  • 32
  • 54
0

See this question: Detect between a mobile browser or a PhoneGap application

I made a contribution but there are plenty of answers which may be suitable for you.

Community
  • 1
  • 1
sciritai
  • 3,688
  • 1
  • 17
  • 22
0

I couldn't get this to work in iOS using Cordova 3.3, presumably because of CB-2520 but, as a hack, I modified CDVViewController.h to make the userAgent property readwrite, and then simply updated self.userAgent in the viewDidLoad of my controller that inherits from CDVViewController.

If timing is an issue, you could forcibly manipulate the userAgent lazy-loaded property in CDVViewController.m.

Both of these are very dirty. :)

Dharmesh Dhorajiya
  • 3,976
  • 9
  • 30
  • 39
Jason
  • 2,223
  • 2
  • 20
  • 28
  • [CB-2520](https://issues.apache.org/jira/browse/CB-2520) has been resolved; CDVViewController now has a baseUserAgent property that can be modified – Mike MacMillan Dec 30 '14 at 19:54