Search code examples
javascripthtmlcordovacordova-plugins

Open itunes window inside ios cordova application


i`m using apache cordova to build ios applications from some html, css and js. At this time i need to open other application inside opened application, for this purposes i use startapp plugin. Everything is ok, but i want opening itunes window inside app with link on application that need to open if application not installed. Here is function that i wrote, but it not give me the result that i want:

function appToApp(identifier, iTunesUrl, success, fail) {
    var app;

    // Init app starter
    if(device.platform === 'iOS') {
        app = startApp.set(identifier);
    } else if(device.platform === 'Android') {
        app = startApp.set({
            "package": identifier
        });
    }


    console.log('Checking ' + identifier + ' application existence...');
    app.check(function(values) {
        console.log('ok, application installed!');
        console.log(values);

        // Run application
        console.log('Running ' + identifier + ' application...');

        // Start application
        app.start(function() {
            console.log('App with identifier = ' + identifier + ' opened!');
            if (typeof(success) == 'function') {
                success.call();
            }
        }, function(error) {
            console.log('Can not open app with identifier = ' + identifier + '!');
            if (typeof(fail) == 'function') {
                fail.call();
            }
            console.warn(error);
        });

    }, function(error) {
        console.warn('Something wrong with application, checking failed!');

        // Open iTunes
        var iTunes = startApp.set('itunes://');
        iTunes.start(function() {
            console.log('iTunes opened!');
        }, function(error) {
            console.warn('Can not open iTunes!');
        });


        console.warn(error);
    });

}

Thanks in advance


Solution

  • As a result i have used this cordova plugins Custom-URL-scheme, AppAvailability.

    First of all i attached custom scheme to app that need launch form another app:

    cordova plugin add cordova-plugin-customurlscheme --variable URL_SCHEME=mycoolapp
    

    Function that open app:

    /**
     * Run app from other app
     * Dependencies:
     * https://github.com/EddyVerbruggen/Custom-URL-scheme
     * https://github.com/ohh2ahh/AppAvailability
     *
     * @param {string} scheme - scheme part of url
     * @param {string} url - custom application url
     * @param {string} iTunesUrl - url on application that located on iTunes
     */
    function appToApp(scheme, url, iTunesUrl) {
        appAvailability.check(
            // URI Scheme or Package Name
            scheme,
            function() {
                // Success callback
                console.log(scheme + ' is available :)');
                // Open application
                window.open(url, '_system');
            },
            function() {
                // Error callback
                console.log(scheme + ' is not available :(');
    
                if (iTunesUrl) {
                    console.log('Installing app from iTunes');
                    window.open(iTunesUrl, '_system');
                } else {
                    alert("Application " + scheme +  " is not available on this device, please install application first");
                }
            }
        );
    }