Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I use a singleton for an ad banner and call it into view on each ViewDidLoad. This automatically removes it from the previous view.</p> <p>This is for adWhirl, but you should be able to adopt it for just iAD.</p> <p>adWhirlSingleton.h</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #import "AdWhirlDelegateProtocol.h" #import "AdWhirlView.h" @interface adWhirlSingleton : NSObject &lt;AdWhirlDelegate&gt; { AdWhirlView *awView; UIViewController *displayVC; } @property (strong, nonatomic) AdWhirlView *awView; @property (strong, nonatomic) UIViewController *displayVC; +(id)sharedAdSingleton; -(void)adjustAdSize:(CGFloat)x:(CGFloat)y; @end </code></pre> <p>adWhirlSingleton.m</p> <pre><code>#import "adWhirlSingleton.h" @implementation adWhirlSingleton static adWhirlSingleton* _sharedAdSingleton = nil; @synthesize awView, displayVC; +(id)sharedAdSingleton { @synchronized(self) { if(!_sharedAdSingleton) _sharedAdSingleton = [[self alloc] init]; return _sharedAdSingleton; } return nil; } +(id)alloc { @synchronized([adWhirlSingleton class]) { NSAssert(_sharedAdSingleton == nil, @"Attempted to allocate a second instance of a singleton."); _sharedAdSingleton = [super alloc]; return _sharedAdSingleton; } return nil; } -(id)init { self = [super init]; if (self != nil) { // initialize stuff here self.awView = [AdWhirlView requestAdWhirlViewWithDelegate:self]; } return self; } -(void)dealloc { displayVC = nil; if (awView) { [awView removeFromSuperview]; //Remove ad view from superview [awView replaceBannerViewWith:nil]; [awView ignoreNewAdRequests]; // Tell adwhirl to stop requesting ads [awView setDelegate:nil]; awView = nil; } } -(void)adjustAdSize:(CGFloat)x :(CGFloat)y { [UIView beginAnimations:@"AdResize" context:nil]; [UIView setAnimationDuration:0.7]; awView.frame = CGRectMake(x, y, kAdWhirlViewWidth, kAdWhirlViewHeight); [UIView commitAnimations]; NSLog(@"Recent Network Name: %@",[awView mostRecentNetworkName]); } -(BOOL)adWhirlTestMode { return YES; } -(NSString *)adWhirlApplicationKey { return @"xxxxxxxxxxxxx"; } -(UIViewController *)viewControllerForPresentingModalView { return displayVC; } -(void)adWhirlDidReceiveAd:(AdWhirlView *)adWhirlView { NSLog(@"%s",__FUNCTION__); NSLog(@"Recent Network Name: %@",[awView mostRecentNetworkName]); //[self adjustAdSize]; } -(void)adWhirlDidFailToReceiveAd:(AdWhirlView *)adWhirlView usingBackup:(BOOL)yesOrNo { NSLog(@"%s",__FUNCTION__); } @end </code></pre> <p>Then import adWhirlSingleton into each ViewController and in each viewWillAppear i just implement this:</p> <pre><code>adWhirlSingleton *adWhirlSingle = [adWhirlSingleton sharedAdSingleton]; adWhirlSingle.displayVC = self; [adWhirlSingle adjustAdSize:0 :self.view.frame.size.height -50]; [self.view addSubview:adWhirlSingle.awView]; [self.view bringSubviewToFront:adWhirlSingle.awView]; NSLog(@"Ad Banner View"); </code></pre> <p>but the view I have with a UITableView, I use this:</p> <pre><code>adWhirlSingleton *adWhirlSingle = [adWhirlSingleton sharedAdSingleton]; adWhirlSingle.displayVC = self; [adWhirlSingle adjustAdSize:0 :self.tabBarController.view.frame.size.height -99]; [self.tabBarController.view addSubview:adWhirlSingle.awView]; NSLog(@"Should have added Ad!"); </code></pre> <p>Hope that helps you a bit</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload