Or how to create a simple module when both iOS and Android come into play
This is the second part of the Monetize your NativeScript apps with AdMob blog. In part one I discussed how you can easily access and use the native API of a native framework in JavaScript, taking the AdMob library for iOS as an example. To do that, you only needed the existing AdMob documentation in combination with a few simple rules to translate the Objective-C API calls to JavaScript.
The popular mobile platforms, however, are two - iOS and Android. So, it would be fair if I cover Android as well. Moreover, further in this article as things get more interesting and we are close to having a cross-platform AdMob usage, I will show you what’s the recommended way of separating and isolating the native API calls in a NativeScript project. So, let’s begin.
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
() {
// do sth as the user is leaving the app, because of a clicked ad
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
() {
// do sth as the user is leaving the app, because of a clicked ad
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
() {
// do sth as the user is leaving the app, because of a clicked ad
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) {
// do sth as the user is leaving the app, because of a clicked ad
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
() {
// do sth as the user is leaving the app, because of a clicked ad
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
() {
// do sth as the user is leaving the app, because of a clicked ad
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;