简体   繁体   中英

Detect if a javascriptinterface method exists or not

I have an Android Project that is already in play store and being installed on many devices. In my previous version there was not any method in java to check the version of app that was being called from javascript.

Then I developed a new version of app and added a javascriptinterface function to return version of the app installed. Now I want my client to update app if there is old version installed on their device.

Because that javascriptinterface function was not exist in old app, when I call that method for old apps it does not do anything as the method do not exists in old app. Its working fine in new app.

My question is: Is there any way to detect if a javascriptinterface function exists or not from old app version where that method wasn't even defined???

If I am getting this right, than, in your new version, a new method was added for to the javascript engine, by java, such that window.getAppVersion() yields the current version (assuming the method name is indeed »getAppVersion«).

To check if that method exists in Javascript a certain scope ( window in this case), you can do:

if ('getAppVersion' in window) { … }

so you can add something like that to the beginning of the script:

if (!('getAppVersion' in window)) {
  window.getAppVersion = function () {
    //really old verion
    return -Infinity;
  }
}

That way you create the function only if it not exists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM