Cordova 3.x 实用插件(6) -- 检查APP是否被安装

应用中经常要启动其他应用,比如:打开市场为自己的应用打分、强制用户更新应用、强制Chrome打开网页等等。在调用之前,你有必要知道要调用的应用是否在机器上已经安装。(很多通过URL Scheme启动的应用如果没有安装的话就没有任何提示)

这个插件很小,也很简单,但是还是比较实用的。在Android环境下它通PackageManager来检查ID是否存在,在iOS环境下通过canOpenURL检查机器是否安装了该应用。

插件地址: https://github.com/ohh2ahh/AppAvailability

(1)创建工程
引用
cordova create AppAvailability com.rensanning.cordova AppAvailability
cd AppAvailability
cordova platform add android


(2)安装plugin
引用
cordova plugin add org.apache.cordova.device
cordova plugin add https://github.com/ohh2ahh/AppAvailability.git


(3)修改代码
if (device.platform.toLowerCase() == 'ios') {
    appAvailability.check(
        'twitter://', // URI Scheme
        function() {  // Success callback
            alert('Twitter is available');
        },
        function() {  // Error callback
            alert('Twitter is not available');
        }
    );
}

if (device.platform.toLowerCase() == 'android') {
    appAvailability.check(
        'com.twitter.android', // URI Scheme
        function() {           // Success callback
            alert('Twitter is available');
        },
        function() {           // Error callback
            alert('Twitter is not available');
        }
    );
}


(4)编译后安装到手机上
引用
cordova build




猜你喜欢

转载自rensanning.iteye.com/blog/2115705
今日推荐