Here's how I did it - there are a few hoops you have to jump through.
First, you need to add the iAd.framework - in XCode, in the Overview column, control-click on "Frameworks", and in the menu that pops up, move your cursor over "Add", then click on "Existing Frameworks"; click on "iAd.framework". It should now appear below Frameworks in the Overview (along with, presumably, UIKit, Foundation, and CoreGraphics).
Next, make some changes to your main app view controller's .h file:
add an import for <iAd/iAd.h>, add <ADBannerViewDelegate> to the @interface line, and declare an IBOutlet ADBannerView (you will need this to hide/move the Ad Banner in Interface Builder).
Here is an example, assuming your project is named yourApp:
Code:
#import <iAd/iAd.h>
@interface yourAppViewController : UIViewController <ADBannerViewDelegate>
{
IBOutlet ADBannerView *theAdBanner;
}
Next, in your view controller's .m file, add code for the three methods that can be called.
bannerViewActionShouldBegin is called when the app wants to show a full-size ad; if you want to pause your app while the ad is running, you do it here. (Also, if, for some reason, you do not want the ad to display, return NO.) My sample code does nothing other than return YES as my app does not really do anything in the background.
Code:
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
if (!willLeave)
{
// insert code here to suspend any services that might conflict with the advertisement
}
return YES;
}
bannerView:didFailToReceiveAdWithError: is called when an ad cannot be put into the banner for some reason - for example, your WiFi is turned off. In this case, the iAd banner box will remain on the screen but be empty, unless you do something to hide it. This is why your app was rejected - you just let the box sit there. The sample code you had moves it 50 pixels up (which would put it off the top of the screen if it was placed at the top). My code just hides it.
Code:
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
theAdBanner.hidden = YES;
}
bannerViewDidLoadAd is called when an ad is actually put into the banner. I use it to restore the banner if it is hidden.
Code:
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
theAdBanner.hidden = NO;
}
Note that I do not use a variable like the Apple sample code's bannerIsVisible, because I just hide and unhide the banner rather than doing anything fancy. If you want to use a separate variable (so you could move the banner off and on the screen, the way the Apple sample code does), you declare the variable (as a bool) in your class definition in the .h file, and initialize it to YES (since it starts on the screen) in viewDidLoad.
What the code you got does is, in bannerViewDidLoadAd, it checks to see if it is off the screen (by seeing if the variable = NO), and if it is, it changes the banner's Y-coordinate to put it onto the screen, then sets the variable to YES; and, in didFailToReceiveAdWithError, it does the opposite - it checks the variable to see if the banner is on the screen, and if it is, it changes the banner's Y-coordinate to move it off the screen and sets the variable to NO.
-- Don