如何让AdMob Pro在Ionic中运行

时间:2021-05-12 22:58:13

I am getting 'AdMob' is undefined when I try to execute the code below. I am trying to use the Cordova AdMob Pro plugin, but can't get past the undefined error(s). I can't confirm if the plugin is even being loaded.

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

Here are the versions I'm using: AngularJS v1.3.4, Cordova v4.1.2, Ionic v1.2.8. I'm running the code on a Galaxy S5 phone running Android version 4.4.4.

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

I've tried adding this code in both the app.js and the controller.

我已经尝试在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'); }
        );
    }
});

3 个解决方案

#1


1  

Just went through this as well and found that the error is from trying to find 'AdMob' when that doesnt exist on if (window.AdMob || AdMob)

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

if you just use 'if (window.AdMob) {' you should be fine.

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

#2


1  

I just developed an AngularJS extension on top of AdMob Pro so you can interact better with this Cordova Plugin.

我刚刚在AdMob Pro上开发了一个AngularJS扩展,因此您可以更好地与此Cordova插件进行交互。

Here is the repository: https://github.com/santonocito/angular-admobpro

这是存储库:https://github.com/santonocito/angular-admobpro

Here an example of how you can use it with Ionic:

这里有一个如何使用Ionic的例子:

angular.module('yourApp', ['ionic', 'admob'])
    .run(function($rootScope, $state, $log, $adMob) {

        $ionicPlatform.ready(function() {
            // AdMob
            if(window.AdMob) {
                var admobid;

                if (device.platform == "Android") {
                    admobid = { // for Android
                        banner: 'ca-app-pub-your-ad-key',
                        interstitial: 'ca-app-pub-your-ad-key'
                    };
                } else {
                    admobid = { // for iOS
                        banner: 'ca-app-pub-your-ad-key',
                        interstitial: 'ca-app-pub-your-ad-key'
                    };
                }

                $adMob.createBanner( {
                    adId: admobid.banner,
                    autoShow: true,
                    bgColor: 'black',
                    position: $adMob.position.BOTTOM_CENTER
                });

                $adMob.prepareInterstitial({
                    adId: admobid.interstitial,
                    autoShow: false
                });
            }
        });
    });

#3


0  

1.install admob phonegap plugin

1.install admob phonegap插件

cordova plugin add admob
  1. init admob plugin,init plugin after deviceready event

    init admob插件,initready事件后的init插件

    admob.initAdmob("admob banner ID","admob interstitial ID");
    

3.show admob ads

3.显示admob广告

admob.showBanner(admob.BannerSize.BANNER,admob.Position.TOP_APP);//show banner at the top of app

ref:https://github.com/admob-google/admob-cordova

裁判:HTTPS://github.com/admob-google/admob-cordova

#1


1  

Just went through this as well and found that the error is from trying to find 'AdMob' when that doesnt exist on if (window.AdMob || AdMob)

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

if you just use 'if (window.AdMob) {' you should be fine.

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

#2


1  

I just developed an AngularJS extension on top of AdMob Pro so you can interact better with this Cordova Plugin.

我刚刚在AdMob Pro上开发了一个AngularJS扩展,因此您可以更好地与此Cordova插件进行交互。

Here is the repository: https://github.com/santonocito/angular-admobpro

这是存储库:https://github.com/santonocito/angular-admobpro

Here an example of how you can use it with Ionic:

这里有一个如何使用Ionic的例子:

angular.module('yourApp', ['ionic', 'admob'])
    .run(function($rootScope, $state, $log, $adMob) {

        $ionicPlatform.ready(function() {
            // AdMob
            if(window.AdMob) {
                var admobid;

                if (device.platform == "Android") {
                    admobid = { // for Android
                        banner: 'ca-app-pub-your-ad-key',
                        interstitial: 'ca-app-pub-your-ad-key'
                    };
                } else {
                    admobid = { // for iOS
                        banner: 'ca-app-pub-your-ad-key',
                        interstitial: 'ca-app-pub-your-ad-key'
                    };
                }

                $adMob.createBanner( {
                    adId: admobid.banner,
                    autoShow: true,
                    bgColor: 'black',
                    position: $adMob.position.BOTTOM_CENTER
                });

                $adMob.prepareInterstitial({
                    adId: admobid.interstitial,
                    autoShow: false
                });
            }
        });
    });

#3


0  

1.install admob phonegap plugin

1.install admob phonegap插件

cordova plugin add admob
  1. init admob plugin,init plugin after deviceready event

    init admob插件,initready事件后的init插件

    admob.initAdmob("admob banner ID","admob interstitial ID");
    

3.show admob ads

3.显示admob广告

admob.showBanner(admob.BannerSize.BANNER,admob.Position.TOP_APP);//show banner at the top of app

ref:https://github.com/admob-google/admob-cordova

裁判:HTTPS://github.com/admob-google/admob-cordova