How to switch admob view from MainViewController using Cordova
I have applied admob in my iPhone app, but the generated view needs to be switched based on my javascript condition. So, I need to switch this view using cordova plugins. Is it possible to switch admob view using phonegap?
0
user1616591
source
to share
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
RajPara
source
to share