Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display admob ads with a custom size in iPhone

Tags:

iphone

admob

Can I display admob ads with a custom size in iphone, such as 280x50, not 320x50 typically ?

like image 694
John Zhang Avatar asked Dec 12 '22 21:12

John Zhang


1 Answers

Custom Ad Size

In addition to the standard AdMob ad units, DFP allows you to serve any sized ad unit into an application. Note that the ad size (width, height) defined for an ad request should match the dimensions of the ad view displayed on the application (i.e. DFPBannerView).

Example:

// Define custom GADAdSize of 280x30 for DFPBannerView
GADAdSize customAdSize = GADAdSizeFromCGSize(280, 30);
// Don't use autorelease if you are using ARC in your project
self.adBanner = [[[DFPBannerView alloc] initWithAdSize:customAdSize] autorelease];

Note: DFP does not currently support Smart Banners.

Multiple Ad Sizes

DFP allows you to specify multiple ad sizes which may be eligible to serve into a DFPBannerView. There are three steps needed in order to use this feature:

In the DFP UI, create a line item targeting the same ad unit that is associated with different size creatives. In your application, set the validAdSizes property on DFPBannerView:

// Define an optional array of GADAdSize to specify all valid sizes that are appropriate
// for this slot. Never create your own GADAdSize directly. Use one of the
// predefined standard ad sizes (such as kGADAdSizeBanner), or create one using
// the GADAdSizeFromCGSize method.
//
// Note: Ensure that the allocated DFPBannerView is defined with an ad size. Also note
// that all desired sizes should be included in the validAdSizes array.  

GADAdSize size1 = GADAdSizeFromCGSize(CGSizeMake(120, 20));
GADAdSize size2 = GADAdSizeFromCGSize(CGSizeMake(250, 250));
GADAdSize size3 = GADAdSizeFromCGSize(CGSizeMake(320, 50));
NSMutableArray *validSizes = [NSMutableArray array];
[validSizes addObject:[NSValue valueWithBytes:&size1 objCType:@encode(GADAdSize)]];
[validSizes addObject:[NSValue valueWithBytes:&size2 objCType:@encode(GADAdSize)]];
[validSizes addObject:[NSValue valueWithBytes:&size3 objCType:@encode(GADAdSize)]];
bannerView_.validAdSizes = validSizes;

Implement the GADAdSizeDelegate method to detect an ad size change.

@protocol GADAdSizeDelegate <NSObject>
- (void)adView:(GADBannerView *)view willChangeAdSizeTo:(GADAdSize)size;
@end

Remember to set the delegate using the setAdSizeDelegate: before making the request for an ad.

[bannerView_ setAdSizeDelegate:self];

Be sure to set the GADBannerView's adSizeDelegate property to nil before releasing the view:

- (void)dealloc {
 bannerView_.adSizeDelegate = nil;

 // Don't release the bannerView_ if you are using ARC in your project
 [bannerView_ release];
 [super dealloc];
}
like image 170
Vikas S Singh Avatar answered Jan 11 '23 00:01

Vikas S Singh