Tuesday, October 5, 2010

iAd's in Universal App

In iOS 4.2, there is a change in the references to the ADBannerView size. We used to use:
ADBannerContentSizeIdentifier320x50ADBannerContentSizeIdentifier480x32

But now we need to use:
ADBannerContentSizeIdentifierPortraitADBannerContentSizeIdentifierLandscape

This allows iOS 4.2 to resize the iAd based on the device / window size.

So how do you create a universal app that uses iAd's AND will run on iOS 4.0? Here's what I did (based on some help from Rincewind on the Dev forums; I didn't know how to check for an NSString. Now I do! You use &<string> and if that doesn't equal nil, you are good to go).

Overview: I chose to create two new string values and then based on whether or not the app was running on iOS 4.2, set them to the appropriate identifier.

In .h file (view controller)
{
     NSString *PortraitBanner;
     NSString *LandscapeBanner;
}
@property(nonatomic, retain) NSString *PortraitBanner;
@property(nonatomic, retain) NSString *LandscapeBanner;
@end

In .m file
@synthesize PortraitBanner;
@synthesize LandscapeBanner;

in my createBannerView method:
//If true then we're on iOS 4.2 (or newer) and we can use the identifiers
//If not true then we use the old identifiers
if (&ADBannerContentSizeIdentifierPortrait != nil) {
     PortraitBanner = ADBannerContentSizeIdentifierPortrait;
     LandscapeBanner = ADBannerContentSizeIdentifierLandscape;
     NSLog(@"Running on iOS 4.2 or newer.");
} else {
     PortraitBanner = ADBannerContentSizeIdentifier320x50;
     LandscapeBanner = ADBannerContentSizeIdentifier480x32;
     NSLog(@"Running on Pre-iOS 4.2.");
}

//Don't forget to release
in dealloc:
[PortraitBanner release];
[LandscapeBanner release];


Note: If you haven't checked out the Apple sample code iAd Suite, that's where my createBannerView method came from.

Then pretty much wherever you'd use one of the "ADBannerContentSizeIdentifier*" strings, use either PortraitBanner or LandscapeBanner. I've tested my app on iOS 4.1 and iOS 4.2 on several devices and my iPad and it is working fine.

5 comments:

  1. I receive the following when i rotate my device.

    Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'currentContentSize must be one of the requiredContentSizes; 'ADBannerContentSize480x32' is not in {(
    ADBannerContentSizeLandscape,
    ADBannerContentSizePortrait

    thats my standard problem on ios 4.1, no idea where this comes from.

    ReplyDelete
  2. @Hans, sorry I missed you comment.

    I have seen a couple threads on the Apple Dev forums about crashes in iOS 4.1. All I can say is I've tested my code from my live app and it works fine in both 4.1 and 4.2(.1).

    Might want to have a look at the Dev forums, sorry again for missing your comment.

    ReplyDelete
  3. I just had my free iAd supported app released today only to find out that it crashed on iOS 4.0 phones. Your article helped me fix it so thanks. I just hope Apple approves of the changes before too many iOS 4.0 users download it.

    ReplyDelete
  4. @Jax - awesome! ... er wait... no not that your app crashed, rather that the article helped. Thanks for posting and good luck in the store!
    Greg

    ReplyDelete
  5. I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well.
    App Development Company

    ReplyDelete