TypeError:无法读取未定义的属性“AdMob”

时间:2016-01-01 14:19:06

标签: ionic-framework cordova-plugins

我使用的是globhotpot插件来展示广告

$('[id^="shownum"]').click(function(e){
  $(this).toggle();
});

这是我的admob代码

select count(t.id), c.title , t.semester , t.year
from course c
left join takes t on t.cource_id=c.cource_id
group by 2,3,4
having count(t.id) > 50

select c.title, count(s.sec_id) 
from course c
left join section  s on c.course_id = s.course_id
group by 1
having count(s.sec_id) > 1

select i.ID, count(c.id)
from instructor i
left join course c on c.dept_name = i. dept_name
group by 1
having count(c.id)>5

select i.ID
from instructor i
left join department d on d.dept_name=i.dept_name
where d.dept_name<>'Biology'

但是当我使用ng-init =“showBannerAd()调用showBannerAd()时,它不显示广告并显示此错误消息。我怎么解决这个问题?我错过了什么重要的事情吗?

cordova plugin add https://github.com/floatinghotpot/cordova-plugin-admob.git 

这是已安装的插件列表

$ionicPlatform.ready(function($location) {
        var adMobId = {
            admob_banner_key: 'ca-app-pub-3378914045953959/7363511027'
        };

        var options = {
            interstitialAdId: 'ca-app-pub-3940256099942544/1033173712',
            autoShow: true
        };

        var adMobPosition = {
            BOTTOM_CENTER: 8
        };

       $scope.showBannerAd = function() {

            try {
                console.log('Show Banner Ad');
                $cordovaAdMob.createBannerView({
                    adId: adMobId.admob_banner_key,
                    position: adMobPosition.BOTTOM_CENTER,
                    isTesting: true,
                    autoShow: true
                });

            } catch (e) {
                alert(e);
            }
        }

        $scope.showInterstitialAd = function(st) {

            try {
                console.log('Show Interstitial Ad');

                $cordovaAdMob.createInterstitialView(options, function() {
                        admob.requestInterstitialAd({
                                'isTesting': true
                            },
                            function() {
                                admob.showAd(true);
                            },
                            function(error) {
                                console.log('failed to request ad ' + error);
                            }
                        );
                    },
                    function() {
                        console.log('failed to create Interstitial view');
                    });

            } catch (e) {
                alert("ALAS");
            }
        }
    });

2 个答案:

答案 0 :(得分:0)

我已经创建了一个JS文件,如下所示

var admobid = {};

// TODO: replace the following ad units with your own
if( /(android)/i.test(navigator.userAgent) ) { 
  //console.log("android");
  admobid = { // for Android
    banner: 'ca-app-pub-123123/123123',
    interstitial: 'ca-app-pub-123123/123123'
  };
} else if(/(ipod|iphone|ipad)/i.test(navigator.userAgent)) {
  admobid = { // for iOS
    banner: 'ca-app-pub-123123/123123',
    interstitial: 'ca-app-pub-123123/4123123'
  };
} else {
  admobid = { // for Windows Phone
    banner: 'ca-app-pub-123123/123123',
    interstitial: 'ca-app-pub-123123/123123'
  };
}

function initApp() {
  if (! AdMob ) { alert( 'admob plugin not ready' ); return; }

  // this will create a banner on startup
  AdMob.createBanner( {
    adId: admobid.banner,
    position: AdMob.AD_POSITION.BOTTOM_CENTER,
    isTesting: false, // TODO: remove this line when release
    overlap: false,
    offsetTopBar: false,
    bgColor: 'black'
  } );

  // this will load a full screen ad on startup
  AdMob.prepareInterstitial({
    adId: admobid.interstitial,
    isTesting: false, // TODO: remove this line when release
    autoShow: false
  });
}

if(( /(ipad|iphone|ipod|android|windows phone)/i.test(navigator.userAgent) )) {
    document.addEventListener('deviceready', initApp, false);
} else {
    initApp();
}

并将此js文件包含在index.html中,该文件是应用程序的初始页面

希望这可以帮助你。

答案 1 :(得分:-1)

我在Apache Cordova中发现我必须放置一个setTimeout(也称为sleep),并且只在1,2或3秒后执行我的AdMob代码。 例如:

$ionicPlatform.ready(function($location) {
    setTimeout(function()
    {
        var adMobId = {
            admob_banner_key: 'ca-app-pub-3378914045953959/7363511027'
        };

        var options = {
            interstitialAdId: 'ca-app-pub-3940256099942544/1033173712',
            autoShow: true
        };
        ...
        ...
        ...
        etc...
    }, 3000);
});