Change Phonegap User Agent

Modifying User-Agent string on Cordova / PhoneGap application for iOS

It seems that your link is broken, but I think I know what you are asking for. This is actually a relatively new feature for Cordova, coming from this issue.

It was launched in cordova-ios 3.9.0, about a month ago. See these release notes. (Keep in mind that the platform versions like cordova-ios and cordova-android are different than the version number you gave 5.1, which is the CLI version)

You can set a preference in your config.xml file, in the root of your project.

<preference name="AppendUserAgent" value="myapp" />

Will yield something like this:

"Mozilla/5.0 (Linux; Android 4.4.4; Nexus 5 Build/KTU84P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.114 Mobile Safari/537.36 myapp"

While this:

<preference name="OverrideUserAgent" value="Better_be_really_specific_here" />

Will only give you this:

"Better_be_really_specific_here" 

I recommend only using the AppendUserAgent, as if you just use override, you will lose all the rest of the valuable information like device, OS, browser version etc that you can get from the UA.

Change Phonegap User Agent

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.

Change Phonegap User Agent

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.

Changing User-Agent in phonegap app works in simulator but not on device

Why am I getting different outputs from navigator.userAgent on the simulator and my device?

When we do a remote phonegap build, we're effectively zipping up the assets of the project and uploading them, so any changes we make to files outside the www directory of the project are ignored.

The phonegap build service is doing its own build and creating its own AppDelegate.m, without the changes to edit the user agent, which explains why we're not seeing the custom user agent on the device and we are seeing it on the simulator.

Custom user agent string or header without modifying cordova libs

Looks like there's a pull request for something similar (add "cordova/phonegap" to UAS)

https://github.com/apache/cordova-android/pull/10

Here is the heart of it.

So I would extend DroidGap and override public void init(CordovaWebView webView, CordovaWebViewClient webViewClient, CordovaChromeClient webChromeClient) :

...
WebSettings settings = this.appView.getSettings();
String userAgent = settings.getUserAgentString();
// can append or redefine here
userAgent += " PhoneGap/Cordova";
settings.setUserAgentString(userAgent);
...

Then you can use the extended DroidGap and have control over how you define the User Agent String.

Just confirmed this works, here is the full code using the current Cordova implementation:

package com.focusatwill.androidApp;

import org.apache.cordova.CordovaChromeClient;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaWebViewClient;
import org.apache.cordova.DroidGap;
import org.apache.cordova.api.LOG;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.widget.LinearLayout;

public class DroidGapCustom extends DroidGap {

/**
* Initialize web container with web view objects.
*
* @param webView
* @param webViewClient
* @param webChromeClient
*/
public void init(CordovaWebView webView, CordovaWebViewClient webViewClient, CordovaChromeClient webChromeClient) {
LOG.d("EVENT", "Custom DroidGap.init()");

// Set up web container
this.appView = webView;

// Custom addition of user agent string
WebSettings settings = this.appView.getSettings();
String userAgent = settings.getUserAgentString();
// can append or redefine here
userAgent += " PhoneGap/Cordova";
settings.setUserAgentString(userAgent);

this.appView.setId(100);

this.appView.setWebViewClient(webViewClient);
this.appView.setWebChromeClient(webChromeClient);
webViewClient.setWebView(this.appView);
webChromeClient.setWebView(this.appView);

this.appView.setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT,
1.0F));

// Add web view but make it invisible while loading URL
this.appView.setVisibility(View.INVISIBLE);
this.root.addView(this.appView);
setContentView(this.root);

// Clear cancel flag
this.cancelLoadUrl = false;
}

}

Modify CordovaWebView user agent string in android 4.4 (Cordova 4.3) GWT

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];


Related Topics



Leave a reply



Submit