How to switch admob view from MainViewController using Cordova
1 answer
I'm going to assume that when you switch you want you to hide the view. You can also say that you want to request a new declaration, but regardless, I think the logic will be the same.
If you set up your AdMob code as a plugin, you can write some js that call this (you could do that even if you didn't). So the javascript method might look like:
AdMob.prototype.hideAd =
function(options, successCallback, failureCallback) {
var defaults = {
'isHidden': false
};
for (var key in defaults) {
if (typeof options[key] !== 'undefined') {
defaults[key] = options[key];
}
}
cordova.exec(
successCallback,
failureCallback,
'AdMobPlugin',
'hideAd',
new Array(defaults)
);
};
Then, in your own code that handles the AdMob view, you can do something like this:
- (void)hidAd:(NSMutableArray *)arguments
withDict:(NSMutableDictionary *)options {
CDVPluginResult *pluginResult;
NSString *callbackId = [arguments pop];
if (!self.bannerView) {
// Try to prevent requestAd from being called without createBannerView first
// being called.
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
messageAsString:@"AdMobPlugin:"
@"No ad view exists"];
[self writeJavascript:[pluginResult toErrorCallbackString:callbackId]];
return;
}
BOOL isHidden = (BOOL)[[options objectForKey:@"isHidden"] boolValue];
self.bannerView.hidden = isHidden;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self writeJavascript:[pluginResult toSuccessCallbackString:callbackId]];
}
0
source to share