在iPhone中显示具有自定义尺寸的admob广告

Joh*_*ang 4 iphone admob

我可以在iphone中显示具有自定义尺寸的admob广告,例如280x50,而不是320x50吗?

Vik*_*ngh 5

自定义广告尺寸

除了标准的AdMob广告单元,DFP广告管理系统还允许您将任意规模的广告单元投放到应用程序中.请注意,为广告请求定义的广告尺寸(宽度,高度)应与应用程序上显示的广告视图的尺寸(即DFPBannerView)相匹配.

例:

// 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];
Run Code Online (Sandbox Code Playgroud)

注意:DFP广告管理系统目前不支持智能横幅广告.

多个广告尺寸

DFP广告管理系统允许您指定多个广告尺寸,这些广告尺寸可能有资格投放到DFPBannerView中.要使用此功能,需要三个步骤:

在DFP广告管理系统界面中,创建一个定位与不同尺寸广告素材相关联的广告单元的订单项.在您的应用程序中,在DFPBannerView上设置validAdSizes属性:

// 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;
Run Code Online (Sandbox Code Playgroud)

实施GADAdSizeDelegate方法以检测广告尺寸更改.

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

请记住在发出广告请求之前使用setAdSizeDelegate设置委托.

[bannerView_ setAdSizeDelegate:self];
Run Code Online (Sandbox Code Playgroud)

在释放视图之前,请务必将GADBannerView的adSizeDelegate属性设置为nil:

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

 // Don't release the bannerView_ if you are using ARC in your project
 [bannerView_ release];
 [super dealloc];
}
Run Code Online (Sandbox Code Playgroud)