iPhone project: Ad BannerView in table
Question: I heard that Ad BannerView cannot be placed into an UITableView. Why?
Answer: It can be done, but your app may be rejected. Putting iAD into a table (or presenting as a cell background image, content view, or a header view etc.) is basically against the Apple’s guidelines as table views are frequently refreshing and causing more impression numbers, ie. more income for you, than your app deserves.
1. You can create an UIView that holds your UITableView and the AdBannerView as two separate objects (ie. as two subviews), but you’ll “present” it as the only one view on the screen (see visual tricks for details). In this case you use UIViewController (and not UITableViewController) for that parent UIView.
2. You can insert your AdBannerView into a table view cell, but the table must embedded into an UIView that is controlled by an UIViewController… etc.
While using Interface Builder you likely receive “NSInternalInconsistencyException… The NIB data is invalid” error when you place an AD BannerView into an UITableView object. Xcode and Interface Builder does not issue warnings or errors in the building phase though.
Notes:
1. When you are building an universal application that should be compatible with iPad (iOS 3.2) or iPod Touch or iPhone 3.0, 3.1, 3.3:
A./ you have to use weak link references to iAd.framework
How? In Xcode select app target -> Get Info -> General -> Linked Libraries -> iAd.framework -> Type column -> change Required to Weak. Alternative way is to select the Build tab and “All Configurations” there. Navigate to Linking\Other Linker Flags. Click on the add (+) button, and type “-weak_framework iAd”.
B./ do not use IB for inserting banner view into your code, otherwise you will get ‘NSInvalidUnarchiveOperationException’, reason: ‘Could not instantiate class named ADBannerView’ error message,
C./ check out the availability of iAd.framework in your code like this:
// @property (nonatomic, retain) ADBannerView *adView;
// We are in a view controller
// Banner does not work on iPod/iPhone/iPad when iOS is older than 4.0
if( NSClassFromString(@"ADBannerView") != nil ) {
self.adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
}
// ....
if( self.adView ){
// It works in iOS 4.2+:
if ( &ADBannerContentSizeIdentifierPortrait != nil ) {
self.adView.requiredContentSizeIdentifiers = [NSSet setWithObject: ADBannerContentSizeIdentifierPortrait];
self.adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
}else{
// Otherwise in pre iOS 4.2 use depreciated values:
self.adView.requiredContentSizeIdentifiers = [NSSet setWithObjects:
ADBannerContentSizeIdentifier320x50,
ADBannerContentSizeIdentifier480x32,
nil];
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
}
self.adView.delegate = self;
[self.view addSubview: self.adView];
}