angularjs – 如何让AdMob Pro在Ionic中工作

当我尝试执行下面的代码时,我得到’AdMob’未定义.我试图使用
Cordova AdMob Pro插件,但无法通过未定义的错误.我无法确认插件是否被加载.

以下是我正在使用的版本:AngularJS v1.3.4,Cordova v4.1.2,Ionic v1.2.8.我在运行Android 4.4.4版本的Galaxy S5手机上运行代码.

我已经尝试在app.js和控制器中添加此代码.

$ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if(window.cordova && window.cordova.plugins.Keyboard) {
        cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if(window.StatusBar) {
        // org.apache.cordova.statusbar required
        StatusBar.styleDefault();
    }

    console.log('window.cordova.plugins: ' + window.cordova.plugins);  //returns object
    console.log('window.cordova.plugins.AdMob: ' + window.cordova.plugins.AdMob);  //returns undefined
    console.log('window.AdMob: ' + window.AdMob);    //returns undefined
    console.log('AdMob: ' + AdMob); //returns undefined
    console.log('window.AdmobAd: ' + window.AdmobAd); //returns undefined
    console.log('AdmobAd: ' + AdmobAd);  //returns undefined

    if (window.AdMob || AdMob) {
        alert('admob plugin found');
        var admob_key = (device.platform == "Android") ? "ADMOB_KEY" : "IOS_PUBLISHER_KEY";
        var admob = window.AdMob;
        admob.createBannerView(
            {
                'publisherId': admob_key,
                'adSize': admob.AD_SIZE.BANNER,
                'bannerAtTop': false
            },
            function () {
                admob.requestAd(
                    { 'isTesting': false },
                    function () {
                        admob.showAd(true);
                    },
                    function () { console.log('failed to request ad'); }
                );
            },
            function () { console.log('failed to create banner view'); }
        );
    }
});

最佳答案 刚刚经历过这一点,发现错误来自尝试找到’AdMob’时if(window.AdMob || AdMob)上不存在

如果你只是使用’if(window.AdMob){‘你应该没问题.

点赞