您的位置:首页 > 移动开发

ionic开发——检测是否安装某APP,如果安装打开该APP

2017-06-14 09:46 417 查看
首先,我们需要安装cordova插件

cordova plugin add https://github.com/lampaa/com.lampa.startapp.git //打开第三方APP
cordova plugin add cordova-plugin-appavailability --save//检测是否存在第三方App

cordova plugin add cordova-plugin-inappbrowser //应用内置浏览器

config.xml:

//ios
<allow-intent href="baidumap://*/*" />

//android
<allow-intent href="bdapp://*/*" />


首先,我们要先判断是android还是ios平台

第一种:

var u = navigator.userAgent;
var ua = navigator.userAgent.toLowerCase();
if(!!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)){//如果是ios
  //do something
}
if (ua.match(/Android/i) == "android") {//如果是android
  // do something
}


第二种:

// Don't forget to add the cordova-plugin-device plugin for `device.platform`
if(device.platform === 'iOS') {
//ios
}
else if(device.platform === 'Android') {
//android
}

检测完成后就是打开第三方APP了,这里我们拿百度地图为例,完整代码如下:

var scheme;
// Don't forget to add the cordova-plugin-device plugin for `device.platform`
if(device.platform === 'iOS') {
scheme = 'baidu://';
}
else if(device.platform === 'Android') {
scheme = 'com.baidu.BaiduMap';
}

appAvailability.check(
scheme,       // URI Scheme or Package Name
function() {  // Success callback
alert(scheme + ' is available :)');
var sApp = startApp.set({ /* params */
"action":"ACTION_MAIN",
    "category":"CATEGORY_DEFAULT",
    "type":"text/css",
    "package":"com.baidu.BaiduMap",
    "uri":"file://data/index.html",
    "flags":["FLAG_ACTIVITY_CLEAR_TOP","FLAG_ACTIVITY_CLEAR_TASK"],
    // "component": ["com.android.GoBallistic","com.android.GoBallistic.Activity"],
    "intentstart":"startActivity",
}, { /* extras */
"EXTRA_STREAM":"extraValue1",
"extraKey2":"extraValue2"
});
sApp.start(function() { /* success */
alert("OK");
}, function(error) { /* fail */
alert(error);
});

},
function() {  // Error callback
alert(scheme + ' is not available :(');
}
);

ios中用下面这个方法:

var scheme;
// Don't forget to add the cordova-plugin-device plugin for `device.platform`
if(device.platform === 'iOS') {
scheme = 'baidu://';
}
else if(device.platform === 'Android') {
scheme = 'com.baidu.BaiduMap';
}

appAvailability.check(
scheme,       // URI Scheme or Package Name
function() {  // Success callback
alert(scheme + ' is available :)');
var sApp = startApp.set("baidumap://");
sApp.start(function() { /* success */
alert("OK");
}, function(error) { /* fail */
alert(error);
});

},
function() {  // Error callback
alert(scheme + ' is not available :(');
}
);


对于ios9+需要配置白名单,否则检测无效:

<key>LSApplicationQueriesSchemes</key>
<array>
<string>baidumap</string>
</array>


具体打开什么APP,去查找他的scheme就可以了

打开store下载百度地图:

ios:
window.open("https://itunes.apple.com/cn/app/id452186370")

android:
window.open("market://search?q=com.baidu.BaiduMap")
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐