插件需求(上cordova官網下載):
com.lampa.startapp
cordova-plugin-appavailability
cordova-plugin-inappbrowser
代碼:
對於iOS和android平台的判斷
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
}
檢測手機內是否有此安裝的app
appAvailability.check(
'xxxx', // URI Scheme 這里是app的包名,可以搜一些包名查看器,一般都是com.xxx.xxxx格式的
function () { // Success callback
//成功回調函數,可以執行開始app的方法
$scope.startApp();
},
function () { // Error callback
//失敗的回調函數
}
);
開始app的方法
$scope.startapp = function(){
var sApp = startApp.set({ /* params */
"action":"ACTION_MAIN",
"category":"CATEGORY_DEFAULT",
"type":"text/css",
"package":"com.android.GoBallistic",
"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 */
console.log("OK");
}, function(error) { /* fail */
console.log(error);
});
}