Ionic: Launching external app

2019-09-18 06:48发布

问题:

I'm trying to launch an external app from within my Ionic app. For example, Instagram. I'm attempting this with Lampaa's Startapp plug in, and have followed a few previous examples with no success.

Here's the code inside the controller, called on an ng-click="". I've tried to explain in comments what each step is attempting. I'd also like to add an app.availability to check if the app is installed, but couldn't work out how to do this either. Thanks!

$scope.onInstagramClick = function () {

// Check if
if (ionic.Platform.isAndroid()) {
// Using plugin com.lampa.startapp to launch the app for android
navigator.startApp.set({ 
    "package": "com.instagram.android",
}).start();

} else {
    if (ionic.Platform.isIOS() || ionic.Platform.isIPad()) {
               console.log('ios');
               navigator.startApp.set({ 
    "package": "instagram://",
}).start();
    } else {

         console.log('thisfailed.');
      };
    }
}

回答1:

  $scope.onInstagramClick = function () {


if (ionic.Platform.isAndroid()) {
var sApp = startApp.set("com.instagram.android");
sApp.start(function() { /* success */
    console.log("OK");
}, function(error) { /* fail */
    alert("no Instagram found");
});

} else {
    if (ionic.Platform.isIOS() || ionic.Platform.isIPad()) {
var sApp = startApp.set("instagram://");
sApp.start(function() { /* success */
    console.log("OK");
}, function(error) { /* fail */
    alert("no Instagram");
});

}

Tested on iPhone device. I had to do a few things in XCode to make it work. Namely, editing the info.plist of the application to allow external apps to open. This is done by adding LSApplicationQueriesSchemes as another property inside info.plist, here you add "instagram" or whatever app as an "Item". Moreover, you'll need to make sure App Transport Security Settings has a boolean Allow Arbitrary Loads set to YES.