如何将AdMob广告集成到适用于Android和iOS的Cordova项目中? [英] How to integrate AdMob ads into a Cordova Project for both Android and iOS?

查看:284
本文介绍了如何将AdMob广告集成到适用于Android和iOS的Cordova项目中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用最新版本(6)在Cordova中编写一个多平台应用程序,并且在使AdMob广告在iOS和Android上运行时遇到很多麻烦.我已经下载了AdMob的代码示例,但是通过javascript对其进行控制使我感到困惑.我了解一些有关插件体系结构的信息,但似乎无法使其正常工作.

I am writing a multiplatform app in Cordova using the latest version (6) and am having a lot of trouble trying to get AdMob ads to work on iOS and Android. I have downloaded the code samples for AdMob, but controlling it from the javascript stumps me. I understand something about the plugin architecture, but I just can't seem to get it to work.

请帮助.

推荐答案

您最好的选择是为此使用预制插件.正如您所提到的,我在使用Cordova 6的iOS和Android平台上都能很好地工作.

Your best bet is to use a premade plugin for this. I have experience with one that works well for me on both iOS and Android using Cordova 6 as you mentioned.

完整说明位于此处 https://github.com/sunnycupertino/cordova-plugin- admob-simple 或此处 https://www.npmjs.com/包/cordova-plugin-admob-simple

Full instructions are here https://github.com/sunnycupertino/cordova-plugin-admob-simple or here https://www.npmjs.com/package/cordova-plugin-admob-simple

要安装:

cd yourappfolder

cordova plugin add cordova-plugin-admob-simple

如果您使用的是Eclipse,请将google-play-services.jar复制到libs文件夹中.

If you are using Eclipse, copy the google-play-services.jar into the libs folder.

将以下行添加到清单文件中,紧接在结束的应用程序标记之前

Add the following line to the manifest file, just before the ending application tag

<meta-data android:name="com.google.android.gms.version" android:value="8487000" />

现在在您的JavaScript中,添加以下功能:

Now in your javascript, add the following functions:

//initialize the goodies 
function initAd(){
        if ( window.plugins && window.plugins.AdMob ) {
            var ad_units = {
                ios : {
                    banner: 'ca-app-pub-xxxxxxxxxxx/xxxxxxxxxxx',       //PUT ADMOB ADCODE HERE 
                    interstitial: 'ca-app-pub-xxxxxxxxxxx/xxxxxxxxxxx'  //PUT ADMOB ADCODE HERE 
                },
                android : {
                    banner: 'ca-app-pub-xxxxxxxxxxx/xxxxxxxxxxx',       //PUT ADMOB ADCODE HERE 
                    interstitial: 'ca-app-pub-xxxxxxxxxxx/xxxxxxxxxxx'  //PUT ADMOB ADCODE HERE 
                }
            };
            var admobid = ( /(android)/i.test(navigator.userAgent) ) ? ad_units.android : ad_units.ios;

            window.plugins.AdMob.setOptions( {
                publisherId: admobid.banner,
                interstitialAdId: admobid.interstitial,
                adSize: window.plugins.AdMob.AD_SIZE.SMART_BANNER,  //use SMART_BANNER, BANNER, IAB_MRECT, IAB_BANNER, IAB_LEADERBOARD 
                bannerAtTop: false, // set to true, to put banner at top 
                overlap: true, // banner will overlap webview  
                offsetTopBar: false, // set to true to avoid ios7 status bar overlap 
                isTesting: false, // receiving test ad 
                autoShow: false // auto show interstitial ad when loaded 
            });

            registerAdEvents();
            window.plugins.AdMob.createInterstitialView();  //get the interstitials ready to be shown 
            window.plugins.AdMob.requestInterstitialAd();

        } else {
            //alert( 'admob plugin not ready' ); 
        }
}
//functions to allow you to know when ads are shown, etc. 
function registerAdEvents() {
        document.addEventListener('onReceiveAd', function(){});
        document.addEventListener('onFailedToReceiveAd', function(data){});
        document.addEventListener('onPresentAd', function(){});
        document.addEventListener('onDismissAd', function(){ });
        document.addEventListener('onLeaveToAd', function(){ });
        document.addEventListener('onReceiveInterstitialAd', function(){ });
        document.addEventListener('onPresentInterstitialAd', function(){ });
        document.addEventListener('onDismissInterstitialAd', function(){
            window.plugins.AdMob.createInterstitialView();          //REMOVE THESE 2 LINES IF USING AUTOSHOW 
            window.plugins.AdMob.requestInterstitialAd();           //get the next one ready only after the current one is closed 
        });
    }

//display the banner 
function showBannerFunc(){
    window.plugins.AdMob.createBannerView();
}
//display the interstitial 
function showInterstitialFunc(){
    window.plugins.AdMob.showInterstitialAd();
}

从onDeviceReady()调用init()

Call init() from onDeviceReady()

调用showInterstitialFunc()和showBannerFunc()来展示广告.

Call showInterstitialFunc() and showBannerFunc() to show ads.

请记住,在显示插页式广告之前,您必须稍等片刻,因为加载时间很长.

Remember that you must wait a bit before showing the interstitial, as it takes time to load.

希望这会有所帮助.

这篇关于如何将AdMob广告集成到适用于Android和iOS的Cordova项目中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆