KlingonDragon
KlingonDragon

Reputation: 31

get versionName from within a webview

I have a webview and on one of the pages I have an "about the app" page which shows the current version app version. However I fear if I have to change the app at some point in the future the version will become misaligned as I have to update it separately in the php webpage.

Is there some code I could use to get the android:versionName as a variable and output the variable (preferably php but I'm open to using other methods).

I would like it if i could use a simple variable like if($_SERVER['HTTP_X_REQUESTED_WITH'] == "com.klingondragon.fakeappname") { which I use to check if a browser is being used or if it's coming from the app.

Upvotes: 0

Views: 1286

Answers (2)

cjm
cjm

Reputation: 1

Maybe you can get package using this code:

String versionName = this.getPackageManager().getPackageInfo(
                pkName, 0).versionName;

Upvotes: 0

alex
alex

Reputation: 6409

Add a JavascriptInterface that returns the version name.

webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new VersionJSInterface(), "version");

public static class VersionJSInterface {
  @JavascriptInterface
  public String getVersionName(){
    return BuildConfig.VERSION_NAME;
  }
}

And then in your webview's content you can access an object called version.

<script>
  var versionName = version.getVersionName()
</script>

Upvotes: 1

Related Questions