Created
June 14, 2011 00:38
-
-
Save rcaetano/1024093 to your computer and use it in GitHub Desktop.
iAd Modified Implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define SharedAdBannerView ((btcreportAppDelegate *)[[UIApplication sharedApplication] delegate]).adBanner | |
@interface btcreportAppDelegate : NSObject <UIApplicationDelegate> { | |
UIWindow *window; | |
UINavigationController *navigationController; | |
ADBannerView *adBanner; | |
} | |
@property (nonatomic, retain) IBOutlet UIWindow *window; | |
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController; | |
@property (nonatomic, retain) ADBannerView *adBanner; | |
@end | |
// Add the following to the finished launch event | |
adBanner = [[ADBannerView alloc] initWithFrame:CGRectZero]; | |
// Set the autoresizing mask so that the banner is pinned to the bottom | |
self.adBanner.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin; | |
// Since we support all orientations, support portrait and landscape content sizes. | |
// If you only supported landscape or portrait, you could remove the other from this set | |
self.adBanner.requiredContentSizeIdentifiers = (&ADBannerContentSizeIdentifierPortrait != nil) ? | |
[NSSet setWithObjects:ADBannerContentSizeIdentifierPortrait, nil] : | |
[NSSet setWithObjects:ADBannerContentSizeIdentifier320x50, nil]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation HomeViewController | |
@synthesize contentView; | |
-(void) dealloc { | |
// we are being called here when we navigate away from this view controller, | |
// so go ahead and reset our AdBannerView for the next time | |
// | |
ADBannerView *adBanner = SharedAdBannerView; | |
adBanner.delegate = nil; | |
[adBanner removeFromSuperview]; | |
[contentView release]; contentView = nil; | |
[super dealloc]; | |
} | |
- (void)viewDidUnload { | |
// we are being asked to unload our view controller's view, | |
// so go ahead and reset our AdBannerView for the next time | |
// | |
ADBannerView *adBanner = SharedAdBannerView; | |
adBanner.delegate = nil; | |
[adBanner removeFromSuperview]; | |
self.contentView = nil; | |
} | |
- (void)createAdBannerView { | |
// --- WARNING --- | |
// If you are planning on creating banner views at runtime in order to support iOS targets that don't support the iAd framework | |
// then you will need to modify this method to do runtime checks for the symbols provided by the iAd framework | |
// and you will need to weaklink iAd.framework in your project's target settings. | |
// See the iPad Programming Guide, Creating a Universal Application for more information. | |
// http://developer.apple.com/iphone/library/documentation/general/conceptual/iPadProgrammingGuide/Introduction/Introduction.html | |
// --- WARNING --- | |
ADBannerView *adBanner = SharedAdBannerView; | |
// Depending on our orientation when this method is called, we set our initial content size. | |
// If you only support portrait or landscape orientations, then you can remove this check and | |
// select either ADBannerContentSizeIdentifierPortrait (if portrait only) or ADBannerContentSizeIdentifierLandscape (if landscape only). | |
NSString *contentSize; | |
if (&ADBannerContentSizeIdentifierPortrait != nil) | |
{ | |
contentSize = UIInterfaceOrientationIsPortrait(self.interfaceOrientation) ? ADBannerContentSizeIdentifierPortrait : ADBannerContentSizeIdentifierLandscape; | |
} | |
else | |
{ | |
// user the older sizes | |
contentSize = UIInterfaceOrientationIsPortrait(self.interfaceOrientation) ? ADBannerContentSizeIdentifier320x50 : ADBannerContentSizeIdentifier480x32; | |
} | |
// Calculate the intial location for the banner. | |
// We want this banner to be at the bottom of the view controller, but placed | |
// offscreen to ensure that the user won't see the banner until its ready. | |
// We'll be informed when we have an ad to show because -bannerViewDidLoadAd: will be called. | |
CGRect frame; | |
frame.size = [ADBannerView sizeFromBannerContentSizeIdentifier:contentSize]; | |
frame.origin = CGPointMake(0.0f, CGRectGetMaxY(self.view.bounds)); | |
// Now set the banner view's frame | |
adBanner.frame = frame; | |
// Set the delegate to self, so that we are notified of ad responses. | |
adBanner.delegate = self; | |
// Set the autoresizing mask so that the banner is pinned to the bottom | |
adBanner.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin; | |
// Since we support all orientations in this view controller, support portrait and landscape content sizes. | |
// If you only supported landscape or portrait, you could remove the other from this set | |
adBanner.requiredContentSizeIdentifiers = | |
(&ADBannerContentSizeIdentifierPortrait != nil) ? | |
[NSSet setWithObjects:ADBannerContentSizeIdentifierPortrait, ADBannerContentSizeIdentifierLandscape, nil] : | |
[NSSet setWithObjects:ADBannerContentSizeIdentifier320x50, ADBannerContentSizeIdentifier480x32, nil]; | |
// At this point the ad banner is now be visible and looking for an ad. | |
[self.view addSubview:adBanner]; | |
} | |
- (void)layoutForCurrentOrientation:(BOOL)animated | |
{ | |
ADBannerView *adBanner = SharedAdBannerView; | |
CGFloat animationDuration = animated ? 0.2f : 0.0f; | |
// by default content consumes the entire view area | |
CGRect contentFrame = self.view.bounds; | |
// the banner still needs to be adjusted further, but this is a reasonable starting point | |
// the y value will need to be adjusted by the banner height to get the final position | |
CGPoint bannerOrigin = CGPointMake(CGRectGetMinX(contentFrame), CGRectGetMaxY(contentFrame)); | |
CGFloat bannerHeight = 0.0f; | |
// First, setup the banner's content size and adjustment based on the current orientation | |
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) | |
adBanner.currentContentSizeIdentifier = (&ADBannerContentSizeIdentifierLandscape != nil) ? ADBannerContentSizeIdentifierLandscape : ADBannerContentSizeIdentifier480x32; | |
else | |
adBanner.currentContentSizeIdentifier = (&ADBannerContentSizeIdentifierPortrait != nil) ? ADBannerContentSizeIdentifierPortrait : ADBannerContentSizeIdentifier320x50; | |
bannerHeight = adBanner.bounds.size.height; | |
// Depending on if the banner has been loaded, we adjust the content frame and banner location | |
// to accomodate the ad being on or off screen. | |
// This layout is for an ad at the bottom of the view. | |
if (adBanner.bannerLoaded) | |
{ | |
contentFrame.size.height -= bannerHeight; | |
bannerOrigin.y -= bannerHeight; | |
} | |
else | |
{ | |
bannerOrigin.y += bannerHeight; | |
} | |
// And finally animate the changes, running layout for the content view if required. | |
[UIView animateWithDuration:animationDuration | |
animations:^{ | |
contentView.frame = contentFrame; | |
[contentView layoutIfNeeded]; | |
adBanner.frame = CGRectMake(bannerOrigin.x, bannerOrigin.y, adBanner.frame.size.width, adBanner.frame.size.height); | |
}]; | |
} | |
#pragma mark - | |
#pragma mark ADBannerViewDelegate methods | |
- (void)bannerViewDidLoadAd:(ADBannerView *)banner { | |
[self layoutForCurrentOrientation:YES]; | |
} | |
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error { | |
[self layoutForCurrentOrientation:YES]; | |
} | |
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave { | |
return YES; | |
} | |
- (void)bannerViewActionDidFinish:(ADBannerView *)banner { | |
} | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface HomeViewController : UIViewController <ADBannerViewDelegate> { | |
} | |
@property(nonatomic, retain) IBOutlet UIView *contentView; | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment