How to toggle admob view from MainViewController using Cordova

孤街醉人 提交于 2019-12-11 15:20:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!