问题
I have implemented admob in my iPhone app, but the view which was created should be toggled based on my javascript's condition. So, i need to toggle that view using cordova plugins. Is there any possibility of toggling the admob view using phonegap?
回答1:
I'm going to assume that by toggle you mean you want to hide the view. You could also mean you want to request a new ad but regardless I think the logic would be the same.
If you've set up your AdMob code as a plugin, you can write some js that calls into that (you might be able to do this even if you haven'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 native 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]];
}
来源:https://stackoverflow.com/questions/12070879/how-to-toggle-admob-view-from-mainviewcontroller-using-cordova