phonegap admob plugin get banner height in xcode

582 views Asked by At

I'm using admob plugin (in iOS) and it work's great, but I also use google maps and I adjust the map height according to screen height minus header, and now I need to reduce the admob banner height also.

I tried to add a new function for getting banner size and call it from javascript, but I don't get any response.

This is the code I add to the CDVAdMob.m file:

- (GADAdSize)GetAdSize:(CDVInvokedUrlCommand *)command  {
    return self.bannerView.adSize;
}

I also had the function signature to the CDVAdMob.h file:

- (GADAdSize)GetAdSize:(CDVInvokedUrlCommand *)command;

I'm not familiar with objective c code so I don't know if what I did is correct.

In the javascript I called the new function by:

window.plugins.AdMob.createBannerView();
var size = window.plugins.AdMob.GetAdSize();
alert(size);

Can anyone tell me how it is done correctly?

Thanx

1

There are 1 answers

0
Raymond Xie On BEST ANSWER

Just set the following 2 options: { overlap:false, offsetTopBar:true }, then the plugin will automatically deduct the height of status bar and the banner view.

If you are using some other forks and no such feature, try following steps.

Cordova plugin API is a void function:

- (void) YourAPI:(CDVInvokedUrlCommand *)command;

And, Pass data back to javascript with:

[self.commandDelegate sendPluginResult:result callbackId:callbackId];

GADAdSize is a data structure, you need convert it into a JSON object, then send back.

typedef struct GADAdSize {
  CGSize size;
  NSUInteger flags;
} GADAdSize;

So, you code can be:

- (void) GetAdSize:(CDVInvokedUrlCommand *)command {
    //GADAdSize adSize;

    NSMutableDictionary* data = [[NSMutableDictionary alloc] init];
    [data setValue:[NSNumber numberWithFloat:adSize.size.width] forKey:@"width"];
    [data setValue:[NSNumber numberWithFloat:adSize.size.height] forKey:@"height"];
    CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:data];
    [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}

Mapping in AdMob.js:

admobExport.GetAdSize = function(successCallback, failureCallback) {
    cordova.exec( successCallback, failureCallback, 'AdMob', 'GetAdSize', [] );
};

Then call it in javascript:

window.pugins.AdMob.GetAdSize(function(data){
    var width = data.width;
    var height = data.height;
},function(){});