如何将AdMob广告集成到Cordova项目中,同时支持Android和iOS?

8

我正在使用最新版本的Cordova(6)编写一个跨平台应用程序,并且在试图让AdMob广告在iOS和Android上工作时遇到了很多问题。我已经下载了AdMob的代码示例,但是我不知道如何从JavaScript控制它。我了解插件架构的一些内容,但是我似乎无法让它起作用。

请帮帮我。

3个回答

8
你最好使用一个现成的插件来完成这个任务。我有使用过一个插件,它在 Cordova 6 上同时在 iOS 和 Android 平台上运行良好。
完整的说明在这里:https://github.com/sunnycupertino/cordova-plugin-admob-simple 或者在这里:https://www.npmjs.com/package/cordova-plugin-admob-simple 安装方法如下:
cd yourappfolder

cordova plugin add cordova-plugin-admob-simple

如果您正在使用Eclipse,请将google-play-services.jar文件复制到libs文件夹中。
在应用程序标签结束之前,在清单文件中添加以下行:
<meta-data android:name="com.google.android.gms.version" android:value="8487000" />

现在在你的JavaScript中,添加以下函数:
//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()。

使用showInterstitialFunc()和showBannerFunc()来展示广告。

请记得在展示插屏广告前要等待一段时间,因为它需要加载。

希望这可以帮到你。


有没有办法控制用户是否关闭悬赏视频,以防止他通过此插件获得“积分”? - proofzy
7
注意:这个插件会占用你的一些广告收入。 https://github.com/sunnycupertino/cordova-plugin-admob-simple/blob/a58846c1ea14188a4aef44381ccd28ffdcae3bfa/src/android/AdMob.java#L207 - syonip
@syonip 怎么了?if语句检查发布者ID是否包含“xxx”,因此您必须错过某些配置。 - Toni Michel Caubet
你是不是遇到了“错误:找不到包com.google.android.gms.ads”的问题? - Daniel Danielecki

3

2
该用户未明示地收取高达30%的收益。请查看插件的问题列表以获取说明,除非已被所有者删除。 - ir2pid
这个用户没有明确说明,却抽取了高达30%的收入,是哪个用户@ir2pid? - Fuat
1
插件的创建者 - ir2pid

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接