我正在使用最新版本(6)在Cordova中编写多平台应用程序,并且在尝试将AdMob广告投放到iOS和Android上时遇到了很多麻烦。我已经下载了AdMob的代码示例,但是从javascript控制它会让我感到困惑。我对插件架构有所了解,但我似乎无法让它工作。
请帮忙。
答案 0 :(得分:1)
您最好的选择是使用预制插件。我有使用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()来显示广告。
请记住,在显示插页式广告之前必须等一下,因为加载需要时间。
希望这有帮助。
答案 1 :(得分:0)
这对我来说是最简单的方式: https://www.npmjs.com/package/cordova-plugin-admobpro-firebase
我已经在使用它。 IOS和Android完全免费且易于使用...
答案 2 :(得分:0)