Cordova插件之跳转第三方app

created by diwj on 20171205

第一步:安装Cordova插件

cordova plugin add https://github.com/lampaa/com.lampa.startapp.git //非cordova官方插件,打开第三方app
cordova plugin add cordova-plugin-appavailability --save//检测第三方app是否存在

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





第二步:config.xml需要配置权限


1.ios配置
注:只要有ipa文件,改后缀名为zip后可以当成zip解压缩,解压缩后则能看到.plist文件,若看到的文件乱码,可以下载plistEditor或者相关工具查看

 <allow-intent href="A://*/*" />
 //A是Scheme Name,对应.plist文件里的CFBundleURLSchemes
 <allow-intent href="B://*/*" />
 //B是AppName,对应.plist文件里的CFBundleName



2.android配置

 <allow-intent href="C://*/*" />
 //C是app的scheme





第三步:判断系统所属平台,执行不同的代码

//得安装 cordova-plugin-device这个插件,才能使用以下的判断方式
var scheme;  
      if(device.platform === 'iOS') {
          //ios
          scheme="A"//A同上
          iosCheck(scheme)
      }
      else if(device.platform === 'Android') {
          //android
          scheme="D"//D为Package Name
          androidCheck(scheme)
      }



第四步:先检测app是否存在,存在则打开,不存在则打开下载链接


1.iosCheck方法的具体实现

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

          },
          function() {  // Error callback
            console.log(scheme +"不可用")
            var confirmPopup = $ionicPopup.confirm({
              title: '友情提示',
              template: '您尚未安装该app,是否安装', 
              cancelType:'item-customer-bg-lightred',
              okText: '去AppStore',
              okType:'item-customer-bg-darkred',
              cancelText:'暂不安装',
              cancelType: 'item-customer-bg-lightred'
            });
            confirmPopup.then(function(res){
              if(res){
                console.log("安装中......")
                window.open('下载链接地址')
              }else{
                console.log("取消安装......")
              }
            });
          }
        );
        }



2.androidCheck方法的具体实现

function androidCheck(sheme){
 appAvailability.check(
          scheme,       // URI Scheme or Package Name
          function() {  // Success callback
              console.log(scheme + ' is available :)');
              var sApp = startApp.set({ /* params */
                "action":"ACTION_MAIN",
            "category":"CATEGORY_DEFAULT",
            "type":"text/css",
            "package":sheme,
            "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("open  success")
              }, function(error) { /* fail */
                 console.log("open  failed")
              });

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





最后在ios9+中需要设置白名单才可以,否则无法检测

<key>LSApplicationQueriesSchemeskey>
    <array>
      <string>Astring>
      <string>Bstring>
    array>
    //AB的意义同上,配置路径platforms/projectName/XXX-Info.plist

你可能感兴趣的:(原创,ionic,anularjs,cordova插件,跳转第三方app)