Open other app using package ID/name


#1

Is there a way to open an app which is already installed using only package ID/name (Eg. “com.twitter.android” for Twitter) without need of deeplink URL or app URL?

I am currently interested in only Android’s solution. May be I will check regarding iOS later.

With the help of Cordova plugin inAppBrowser, we can open other app using deeplink of that app.
I want to achieve same feature using package ID/name for apps where deeplinks are not available.

Similar question has been asked here.
I have also tried to look at com.lampa.startapp. But still haven’t succeeded in it.


#2

If you have deeplink of the app, go with in App Browser.

I have achieved app triggering functionality using cordova-plugin-app-launcher.
I believe same can be achieved by com.lampa.startApp.

Below are steps taken by me -

  1. Include to project - cordova plugin add cordova-plugin-app-launcher
  2. Install package - npm install cordova-plugin-app-launcher. No need for cordova prepare command.

To avoid blocking compilation error (due to cordova), I have created a start-app.js file with following contents -

module.exports = {
    packageLaunch: function(appSchemeStr){
        // launch app using package name (for Android devices)
        window.plugins.launcher.launch({
                packageName: appSchemeStr
            }, 
            function(){ }, 
            function(){ alert('Failed to open app') }
        );
    },
    uriLaunch: function(appUriStr){
        // launch app using URI (for iOS devices)
        window.plugins.launcher.launch({
                uri: appUriStr
            }, 
            function(){ }, 
            function(){ alert('Failed to open app') }
        );
    },
}

Now import start-app.js in required *.ts file -

import * as launcher from '../../assets/js/start-app';

And now to launching app using package name -

launcher.packageLaunch("com.company.appName"); // replace with correct package name

Along with above code, include Launcher.js created after npm install command under \node_modules\cordova-plugin-app-launcher\www\ folder to index.html just after cordova.js -

<script src="assets/js/Launcher.js"></script>

Above code works perfectly in devices, but for web, ionic serve command generates warning error message due to Launcher.js as cordova.js is not present, but works there as well when ignored.

To solve that, I have added conditional include statement for Launcher.js in head of index.html.
As this feature is not required in web version, I am only including it in mobile device .
In your *.ts file (mine is home.ts) -

import { Device } from '@ionic-native/device';

export class HomePage{

  constructor(public device: Device) { }

  ionViewDidEnter(){ 
    if(this.device.cordova){
      // This is a mobile device, add app-launcher script in head
      var head = document.getElementsByTagName('head')[0];
      var scriptTag = document.createElement("script"); 

      scriptTag.src = "assets/js/Launcher.js"; // script for launching apps
      head.appendChild(scriptTag);
    }
  }
}

I have confirmed that -
ionic serve,
ionic cordova build android --prod,
ionic cordova run browser and
npm run build --prod work perfectly.


#3

I believe custom url scheme is an option as well, and doesn’t require deeplinks (99% sure) if I understand your question correctly


#4

@jaydz, yes you are correct.

But those apps which I am trying to trigger programmatically are quite old and original developer(s) didn’t created cutom URL scheme for them.


#5

Ah. That does present some limitations for sure. @sujit77 's solution is great, just providing another possibility. Happy coding.


#6

thank you so much the code works perfectly