【问题标题】:Phonegap Build in iphone exec errorPhonegap Build in iphone exec错误
【发布时间】:2014-07-12 17:04:55
【问题描述】:

我在这里遇到了一个奇怪的问题,我正在尝试使用 PhoneGap-InAppPurchase-iOS 将应用购买功能添加到我的 phonegap 应用程序中。但是,在 iphone 上加载时,设备总是显示 'TypeError: 'undefined' is not an object (evaluating 'cordova.exec'),我认为它正在停止我的 init 运行。

当我尝试购买时,我收到错误' InAppPurchase[js]: purchase error: product needs to be loaded before purchase, call storekit.load(...) first!',但这是我的 index.js - 你可以从这里看到我首先运行 storekit.load。我不知道这两个问题是否相关,这是否会阻止其余的 JS 运行?据我所知,我没有运行cordova.exec?

var app = {

    initialize: function() {
        this.bindEvents();
        console.log("Attempting to init");
        document.addEventListener('deviceready', this.onDeviceReady, false);
        try {
            FB.init({appId: "sdfsdfsdfsdfsd", nativeInterface: CDV.FB, useCachedDialogs: false});
            console.log("FB Ready");
        } catch (e) {
            alert(e);
        }

        inappbilling.init(bSuccess, bError, {showLog:true}, "asdass");

    },
    bindEvents: function() {


    },

    onDeviceReady: function() {

        app.receivedEvent('deviceready');
        window.storekit.init({

            debug: true, /* Because we like to see logs on the console */

            purchase: function (transactionId, productId) {
                console.log('purchased: ' + productId);
            },
            restore: function (transactionId, productId) {
                console.log('restored: ' + productId);
            },
            restoreCompleted: function () {
                console.log('all restore complete');
            },
            restoreFailed: function (errCode) {
                console.log('restore failed: ' + errCode);
            },
            error: function (errno, errtext) {
                console.log('Failed: ' + errtext);
            },
            ready: function () {
                var productIds = [
                    "com.asdad.asdads.asdasd"
                ];
                window.storekit.load(productIds, function(validProducts, invalidProductIds) {
                    $.each(validProducts, function (i, val) {
                        console.log("id: " + val.id + " title: " + val.title + " val: " + val.description + " price: " + val.price);
                    });
                    if(invalidProductIds.length) {
                        console.log("Invalid Product IDs: " + JSON.stringify(invalidProductIds));
                    }
                });
            }
        });

    },
    tokenHandler:function(msg) {
        localStorage.setItem("Device", "ios");
        console.log("Token Handler " + msg);
        localStorage.setItem("PushID", msg)

    },
    errorHandler:function(error) {
        console.log("Error Handler  " + error);
        alert(error);
    },
    // result contains any message sent from the plugin call
    successHandler: function(result) {

    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var pushNotification = window.plugins.pushNotification;

        if (device.platform == 'android' || device.platform == 'Android') {
            pushNotification.register(this.successHandler, this.errorHandler,{"senderID":"asdasdadsasd","ecb":"app.onNotificationGCM"});
        }
        else {

            pushNotification.register(this.tokenHandler,this.errorHandler,{"badge":"true","sound":"true","alert":"true","ecb":"app.onNotificationAPN"});
        }
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    },
    // iOS
    onNotificationAPN: function(event) {
        var pushNotification = window.plugins.pushNotification;
        console.log("Received a notification! " + event.alert);
        console.log("event sound " + event.sound);
        console.log("event badge " + event.badge);
        console.log("event " + event);
        if (event.alert) {
            navigator.notification.alert(event.alert);

        }

        if (event.sound) {
            var snd = new Media(event.sound);
            snd.play();
        }
    },
    // Android
    onNotificationGCM: function(e) {
        switch( e.event )
        {
            case 'registered':
                if ( e.regid.length > 0 )
                {
                    console.log("Regid " + e.regid);
                  localStorage.setItem("PushID", e.regid)

                }
                break;

            case 'message':
                // this is the actual push notification. its format depends on the data model
                // of the intermediary push server which must also be reflected in GCMIntentService.java
                alert(e.message);
                break;

            case 'error':
                alert('GCM error = '+e.msg);
                break;

            default:
                alert('An unknown GCM event has occurred');
                break;
        }
    }

};
function bSuccess(result) {
    alert("woo - initialised");
}

function bError(error) {
    alert(error);
}

function updateID() {
    console.log("Updating ID on server");

    var Username = localStorage.getItem("Username");
    var SessionKey = localStorage.getItem("SessionID");
    var PushID =  localStorage.getItem("PushID");
    alert(PushID);
    $.mobile.loading( 'show', { theme: "b", text: "Loading", textonly: false});
    $.ajax({
        crossDomain: true,
        contentType: "application/json; charset=utf-8",
        url: "http://taasdasdasdasdgi.asdasdasdasdasd.com/Register.asmx/RegisterDevice",
        data: {  Username: Username, SessionKey: SessionKey, PushId:PushID, UUid:"moo", ProductType: localStorage.getItem("Device") },
        dataType: "jsonp",
        success: onDataReceived
    });

    function onDataReceived(data)
    {

        $.mobile.loading( 'hide', { theme: "b", text: "", textonly: false});

    }
}
function successHandler1 (result) {
    console.log(result);
    var strResult = "";
    if(typeof result === 'object') {
        strResult = JSON.stringify(result);
    } else {
        strResult = result;
    }

}
function errorHandler1 (error) {
    console.log(error);
    alert("ERROR: \r\n"+error );
}
function subscribe(){
    // make the purchase
    if (device.platform == 'android' || device.platform == 'Android') {
        inappbilling.buy(successHandler1, errorHandler1,"asdasdsa");
    }
    else {
        window.storekit.purchase("com.asdsaddas.asdasd.asdasdasd", 1);
    }
    console.log("Trying to buy");

【问题讨论】:

    标签: javascript android ios cordova


    【解决方案1】:

    我面临同样的问题。我已经通过以下步骤解决了这个问题。 (1)。将“设备就绪”事件绑定到您的应用。这段代码应该写在 index.js 主文件中。

    document.addEventListener("deviceready", onDeviceReady , false);
    

    (2)。然后将“onDeviceReady”函数的代码放在index.js文件中。 例如:

        function onDeviceReady(){ 
            window.storekit.init({
    
            debug: true, /* Because we like to see logs on the console */
    
            purchase: function (transactionId, productId) {
                console.log('purchased: ' + productId);
            },
            restore: function (transactionId, productId) {
                console.log('restored: ' + productId);
            },
            restoreCompleted: function () {
                console.log('all restore complete');
            },
            restoreFailed: function (errCode) {
                console.log('restore failed: ' + errCode);
            },
            error: function (errno, errtext) {
                console.log('Failed: ' + errtext);
            },
            ready: function () {
                var productIds = [
                    "com.asdad.asdads.asdasd"
                ];
                window.storekit.load(productIds, function(validProducts, invalidProductIds) {
                    $.each(validProducts, function (i, val) {
                        console.log("id: " + val.id + " title: " + val.title + " val: " + val.description + " price: " + val.price);
                    });
                    if(invalidProductIds.length) {
                        console.log("Invalid Product IDs: " + JSON.stringify(invalidProductIds));
                    }
                });
            }
        });
    
    }
    }
    

    这对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-07
      • 2015-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多