I have an Android Phonegap/Cordova app that interacts with an ASP.NET MVC web application. I am trying to detect server side whether or not the web page is being loaded from a browser or from my Phonegap/Cordova app.
There are a few methods I have considered:
- Change the User Agent string. I found this stackoverflow link that describes a way to make that work. Unfortunately, it did not work for me. The request object did not have the custom user agent string.
- Include a custom header value. This can be done by modifying the cordova library (see this stackoverflow link.) Modifying libraries is usually a bad idea, though, since it becomes a maintenance problem in the future. (Update: this method didn’t work after the first request.)
- Add something special to the query string when loading the first page. A cookie could then be set. This is kind of an ugly solution compared to a simple header change.
Am I doing something wrong on the user agent string change? Or is there another method that would accomplish this?
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) :
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: