0

I use the below code as many posts suggest to modify the user agent string the CordovaWebView will use. However I never receive this user agent string in any of the http requests to the server. I get the usual user agent string.

public class MainActivity extends CordovaActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.init();
    CordovaWebView.setWebContentsDebuggingEnabled(true);
    loadUrl(launchUrl);
}

@Override
protected CordovaWebView makeWebView() {
  CordovaWebView cordovaWebView = super.makeWebView();
  WebSettings settings = cordovaWebView.getSettings();
  settings.setUserAgentString(settings.getUserAgentString()+" phonegap.env=yes");
  settings.setAppCacheEnabled(true);
  settings.setAppCachePath(getApplicationContext().getDir("html5-cache",MODE_PRIVATE).getAbsolutePath());
  return cordovaWebView;
}

I'm loading a GWT application from the server into an Android Cordova container. The GWT host page specifies a cache manifest. The call to the server for the cache manifest from the CordovaWebView does not contain the modified user agent string.

Any ideas why not?

I'm trying to detect whether the application is running in a Cordova container or the mobile browser by analyzing the user agent string received on the server so that I can serve up the correct cache manifest.

paul
  • 494
  • 6
  • 17

1 Answers1

1

I am Looking for the same solution. For now I found the following options.

It seems the dev are working on feature that will enable it safely.

If you cant wait there is another way.

Update - My temp solution until CB-3360 is out

To change the User Agent I edited the file: CDVViewController.m at line 614 (on IOS)

From:

_userAgent = [NSString stringWithFormat:@"%@ (%lld)", localBaseUserAgent, (long long)self];

to:

_userAgent = [NSString stringWithFormat:@"%@ (%lld) MY EXTRA STRING", localBaseUserAgent, (long long)self];
michaelbn
  • 7,393
  • 3
  • 33
  • 46
  • Out of interest you are implying that window.location.assign = is causing the CordovaWebView to launch the in app browser ?? Is that correct? What happens if I do not have the in app browser installed as a plugin – paul May 20 '15 at 09:01
  • Actually I'm not sure if this is the issue. In my CordovaWebView I do window.location.assign = . The that loads up still has access to the Cordova API's which suggests to me I am not opening an inAppBrowser – paul May 20 '15 at 10:29
  • I am still experimenting. I also trying to include required js files using cdvfile://. If that works then safari browser will just ignore it and then I wont be needing the User Agent modification. – michaelbn May 20 '15 at 10:49
  • 1
    for now I've given up. I've instead added to the cache manifest so my server can detect it is phonegap. I also add a meta tag to the page so that GWT can determine it is running in phonegap. Not the best workaround but it seems to work – paul May 20 '15 at 11:39