或者如何在 iOS 和 Android 都涉及的情况下创建一个简单的模块
这是“使用 AdMob 货币化您的 NativeScript 应用”博客的第二部分。在第一部分中,我讨论了如何在 JavaScript 中轻松访问和使用原生框架的原生 API,并以 iOS 的 AdMob 库为例。为此,您只需要现有的 AdMob 文档,并结合一些简单的规则将 Objective-C API 调用转换为 JavaScript。
然而,流行的移动平台有两个——iOS 和 Android。因此,如果我也涵盖 Android 则会很公平。此外,在本文的后面,随着事情变得越来越有趣,并且我们即将实现跨平台的 AdMob 使用,我将向您展示在 NativeScript 项目中分离和隔离原生 API 调用的推荐方法。因此,让我们开始吧。
function
creatingView(args) {
if
(platformModule.device.os == platformModule.platformNames.ios) {
bannerView = GADBannerView.alloc().initWithAdSize(kGADAdSizeSmartBannerPortrait);
args.view = bannerView;
}
else
{
var
bannerView =
new
com.google.android.gms.ads.AdView(args.object._context);
bannerView.setAdSize(com.google.android.gms.ads.AdSize.SMART_BANNER);
args.view = bannerView;
}
}
exports.creatingView = creatingView;
bannerView = placeholder.android;
bannerView.setAdUnitId(
"ca-app-pub-3940256099942544/6300978111"
);
var
MyAdListener = com.google.android.gms.ads.AdListener.extend(
{
onAdLeftApplication:
function
() {
// 当用户因点击广告而离开应用时执行某些操作
console.log(
"Leaving the app, bye bye!"
);
}
});
var
listener =
new
MyAdListener();
bannerView.setAdListener(listener);
var
adRequest =
new
com.google.android.gms.ads.AdRequest.Builder();
adRequest.addTestDevice(com.google.android.gms.ads.AdRequest.DEVICE_ID_EMULATOR);
var
requestBuild = adRequest.build();
bannerView.loadAd(requestBuild);
function
pageLoaded(args) {
var
page = args.object;
page.bindingContext = vmModule.mainViewModel;
var
placeholder = page.getViewById(
"bannerView"
);
var
bannerView;
if
(platformModule.device.os == platformModule.platformNames.ios) {
bannerView = placeholder.ios;
bannerView.adUnitID =
"ca-app-pub-3940256099942544/2934735716"
;
bannerView.strongDelegateRef = bannerView.delegate = GADBannerViewDelegateImpl.
new
();
bannerView.rootViewController = page.ios;
var
request = GADRequest.request();
request.testDevices = [kGADSimulatorID];
bannerView.loadRequest(request);
}
else
{
bannerView = placeholder.android;
bannerView.setAdUnitId(
"ca-app-pub-3940256099942544/6300978111"
);
var
MyAdListener = com.google.android.gms.ads.AdListener.extend(
{
onAdLeftApplication:
function
() {
// 当用户因点击广告而离开应用时执行某些操作
console.log(
"Leaving the app, bye bye!"
);
}
});
var
listener =
new
MyAdListener();
bannerView.setAdListener(listener);
var
adRequest =
new
com.google.android.gms.ads.AdRequest.Builder();
adRequest.addTestDevice(com.google.android.gms.ads.AdRequest.DEVICE_ID_EMULATOR);
var
requestBuild = adRequest.build();
bannerView.loadAd(requestBuild);
}
}
exports.pageLoaded = pageLoaded;
var
interstitial =
new
com.google.android.gms.ads.InterstitialAd(page._context);
interstitial.setAdUnitId(
"ca-app-pub-3940256099942544/1033173712"
);
var
MyAdListener = com.google.android.gms.ads.AdListener.extend(
{
onAdClosed:
function
() {
loadAndroidAd(interstitial);
},
onAdLeftApplication:
function
() {
// 当用户因点击广告而离开应用时执行某些操作
console.log(
"Leaving the app, bye bye!"
);
}
});
var
listener =
new
MyAdListener();
interstitial.setAdListener(listener);
loadAndroidAd(interstitial);
frameModule.topmost().currentPage.interstitial = interstitial;
function
loadAndroidAd(interstitial) {
var
adRequest =
new
com.google.android.gms.ads.AdRequest.Builder();
adRequest.addTestDevice(com.google.android.gms.ads.AdRequest.DEVICE_ID_EMULATOR);
var
requestBuild = adRequest.build();
interstitial.loadAd(requestBuild);
}
function
buttonTapped(args) {
var
interstitial = frameModule.topmost().currentPage.interstitial;
if
(platformModule.device.os == platformModule.platformNames.ios) {
if
(interstitial.isReady) {
interstitial.presentFromRootViewController(args.object.page.frame.ios.controller);
}
}
else
{
if
(interstitial.isLoaded()) {
interstitial.show();
}
}
}
function
createBanner(args) {
var
bannerView = GADBannerView.alloc().initWithAdSize(kGADAdSizeSmartBannerPortrait);
return
bannerView;
}
function
loadBanner(placeholder) {
bannerView = placeholder.ios;
bannerView.adUnitID =
"ca-app-pub-3940256099942544/2934735716"
;
bannerView.strongDelegateRef = bannerView.delegate = GADBannerViewDelegateImpl.
new
();
bannerView.rootViewController = placeholder.page;
var
request = GADRequest.request();
request.testDevices = [kGADSimulatorID];
bannerView.loadRequest(request);
}
var
GADBannerViewDelegateImpl = (
function
(_super) {
__extends(GADBannerViewDelegateImpl, _super);
function
GADBannerViewDelegateImpl() {
_super.apply(
this
, arguments);
}
GADBannerViewDelegateImpl.prototype.adViewWillLeaveApplication =
function
(bannerView) {
// 当用户因点击广告而离开应用时执行某些操作
console.log(
"Leaving the app, bye bye!"
);
};
GADBannerViewDelegateImpl.ObjCProtocols = [GADBannerViewDelegate];
return
GADBannerViewDelegateImpl;
})(NSObject);
exports.createBanner = createBanner;
exports.loadBanner = loadBanner;
function
createBanner(args) {
var
bannerView =
new
com.google.android.gms.ads.AdView(args.object._context);
bannerView.setAdSize(com.google.android.gms.ads.AdSize.SMART_BANNER);
return
bannerView;
}
function
loadBanner(placeholder) {
bannerView = placeholder.android;
bannerView.setAdUnitId(
"ca-app-pub-3940256099942544/6300978111"
);
var
MyAdListener = com.google.android.gms.ads.AdListener.extend(
{
onAdLeftApplication:
function
() {
// 当用户因点击广告离开应用程序时执行某些操作
console.log(
"Leaving the app, bye bye!"
);
}
});
var
listener =
new
MyAdListener();
bannerView.setAdListener(listener);
var
adRequest =
new
com.google.android.gms.ads.AdRequest.Builder();
adRequest.addTestDevice(com.google.android.gms.ads.AdRequest.DEVICE_ID_EMULATOR);
var
requestBuild = adRequest.build();
bannerView.loadAd(requestBuild);
}
exports.createBanner = createBanner;
exports.loadBanner = loadBanner;
var
vmModule = require(
"./main-view-model"
);
var
bannerModule = require(
"./bannerview"
);
function
pageLoaded(args) {
var
page = args.object;
page.bindingContext = vmModule.mainViewModel;
var
placeholder = page.getViewById(
"bannerView"
);
bannerModule.loadBanner(placeholder);
}
function
creatingView(args) {
args.view = bannerModule.createBanner(args);
}
exports.pageLoaded = pageLoaded;
exports.creatingView = creatingView;
var
frameModule = require(
"ui/frame"
);
function
createInterstitial() {
var
currentPage = frameModule.topmost().currentPage;
currentPage.delegate = GADInterstitialDelegateImpl.
new
();
currentPage.interstitial = createAndLoadiOSInterstitial();
}
function
showInterstitial(args) {
var
interstitial = frameModule.topmost().currentPage.interstitial;
if
(interstitial.isReady) {
interstitial.presentFromRootViewController(args.object.page.frame.ios.controller);
}
}
function
createAndLoadiOSInterstitial() {
var
interstitial = GADInterstitial.alloc().initWithAdUnitID(
"ca-app-pub-3940256099942544/4411468910"
);
var
request = GADRequest.request();
interstitial.strongDelegateRef = interstitial.delegate = frameModule.topmost().currentPage.delegate;
request.testDevices = [kGADSimulatorID];
interstitial.loadRequest(request);
return
interstitial;
}
var
GADInterstitialDelegateImpl = (
function
(_super) {
__extends(GADInterstitialDelegateImpl, _super);
function
GADInterstitialDelegateImpl() {
_super.apply(
this
, arguments);
}
GADInterstitialDelegateImpl.prototype.interstitialDidDismissScreen =
function
(gadinterstitial) {
frameModule.topmost().currentPage.interstitial = createAndLoadiOSInterstitial();
};
GADInterstitialDelegateImpl.ObjCProtocols = [GADInterstitialDelegate];
return
GADInterstitialDelegateImpl;
})(NSObject);
exports.createInterstitial = createInterstitial;
exports.showInterstitial = showInterstitial;
var
frameModule = require(
"ui/frame"
);
function
createInterstitial(page) {
var
currentPage = frameModule.topmost().currentPage;
var
interstitial =
new
com.google.android.gms.ads.InterstitialAd(currentPage._context);
interstitial.setAdUnitId(
"ca-app-pub-3940256099942544/1033173712"
);
var
MyAdListener = com.google.android.gms.ads.AdListener.extend(
{
onAdClosed:
function
() {
loadAndroidAd(interstitial);
},
onAdLeftApplication:
function
() {
// 当用户因点击广告离开应用程序时执行某些操作
console.log(
"Leaving the app, bye bye!"
);
}
});
var
listener =
new
MyAdListener();
interstitial.setAdListener(listener);
loadAndroidAd(interstitial);
currentPage.interstitial = interstitial;
}
function
showInterstitial(args) {
var
interstitial = frameModule.topmost().currentPage.interstitial;
if
(interstitial.isLoaded()) {
interstitial.show();
}
}
function
loadAndroidAd(interstitial) {
var
adRequest =
new
com.google.android.gms.ads.AdRequest.Builder();
adRequest.addTestDevice(com.google.android.gms.ads.AdRequest.DEVICE_ID_EMULATOR);
var
requestBuild = adRequest.build();
interstitial.loadAd(requestBuild);
}
exports.createInterstitial = createInterstitial;
exports.showInterstitial = showInterstitial;
var
vmModule = require(
"./main-view-model"
);
var
platformModule = require(
"platform"
);
var
frameModule = require(
"ui/frame"
);
var
interstitialModule = require(
"./interstitial"
);
function
pageLoaded(args) {
var
page = args.object;
page.bindingContext = vmModule.mainViewModel;
interstitialModule.createInterstitial();
}
function
buttonTapped(args) {
interstitialModule.showInterstitial(args);
}
exports.pageLoaded = pageLoaded;
exports.buttonTapped = buttonTapped;