在ios cordova应用程序中打开itunes窗口

时间:2016-04-23 18:23:51

标签: javascript html cordova cordova-plugins

我使用apache cordova从一些html,css和js构建ios应用程序。这时我需要在打开的应用程序中打开其他应用程序,为此我使用startapp插件。一切都很好,但我想在应用程序中打开itunes窗口,如果应用程序未安装,需要打开应用程序上的链接。这是我写的功能,但它没有给我我想要的结果:

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);
    });

}

提前致谢

1 个答案:

答案 0 :(得分:0)

结果我使用了这个cordova插件Custom-URL-schemeAppAvailability

首先,我将自定义方案附加到需要从另一个应用程序启动的应用程序:

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

打开应用程序的功能:

/**
 * 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");
            }
        }
    );
}