iAds are going to be all over apps in the AppStore in just a few short weeks as developers try to make some money from the average iPhone user. The only problem is that you won’t make any money unless the user is running iOS 4.0. Versions of the iOS prior to this don’t have the iAd framework and therefore can’t display ads. So what’s a developer who wants a few clams to do?
Here’s my suggestion if you’re really that concerned about making money:
- Target your app at iOS 3 (unless your analytics show heavy iOS 2 usage, it’s time to leave them).
- Limit a few “pro” features in the iOS 3 version that can be unlocked with a minimal in-app purchase.
- Once your app is ready, weak link the iAd framework, enable iAds in your app, and use intelligent conditional checks when writing iAd code.
Hopefully since you’re choosing to use ads while under iOS 4, you won’t charge to download your app and all features of your app will be unlocked when displaying ads. It still might be appropriate to create an in-app purchase that disables advertisements. Just don’t take advantage of your users to line your pockets.
Unfortunately, you can’t use Interface Builder to add the ADBannerView object so you must generate the element programmatically. Remember to weak link the iAd framework by selecting the appropriate target in the Groups and Files pane and then change “Required” to “Weak” for iAd.
/* YourViewController.h */
#import <iAd/iAd.h>
@interface YourViewController : UIViewController
{
ADBannerView *adView;
BOOL bannerIsVisible;
}
@property(nonatomic, retain) ADBannerView *adView;
@property(nonatomic, assign) BOOL bannerIsVisible;
/* YourViewController.m */
@synthesize adView;
@synthesize bannerIsVisible;
(void)viewDidLoad { [super viewDidLoad];
static NSString * const kADBannerViewClass = @“ADBannerView”; if (NSClassFromString(kADBannerViewClass) != nil) { if (self.adView == nil) { self.adView = [[[ADBannerView alloc] init] autorelease]; self.adView.delegate = self; self.adView.frame = CGRectMake(0, -50, 320, 50); self.adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50; [self.view addSubview:self.adView]; } } }
#pragma mark - #pragma mark ADBannerViewDelegate Methods
(void)bannerViewDidLoadAd:(ADBannerView *)banner { if (!self.bannerIsVisible) { [UIView beginAnimations:nil context:NULL]; banner.frame = CGRectOffset(banner.frame, 0, 50); [UIView commitAnimations]; self.bannerIsVisible = YES; } }
(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error { if (self.bannerIsVisible) { [UIView beginAnimations:nil context:NULL]; banner.frame = CGRectOffset(banner.frame, 0, -50); [UIView commitAnimations]; self.bannerIsVisible = NO; } }
(void)dealloc { self.adView.delegate = nil self.adView = nil;
[super dealloc]; }
Nothing special needs to be done about your interface file (.h). Think about it: at compile time, the preprocessor directive importing the iAd interface defines the interface ADBannerView. Because we weak link the framework, the compiler won’t complain about the implementation not being around for the 3.0 target. You also don’t need to worry about doing checks for the ADBannerView class in the delegate methods, they’ll only get called if adView is instantiated and sets YourViewController as the delegate.
Remember to set your Base SDK for to iOS 4 and your Deployment Target to the lowest iOS version you want to support, or you did all this for naught.