How to create a global reference for iAd and implement in multiple Viewcontrollers

筅森魡賤 提交于 2019-12-01 04:40:13

Just create a Pointer to iAD in APP delegate.

.h:

@property (strong, nonatomic) ADBannerView *UIiAD;

.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    UIiAD = [[ADBannerView alloc] init];
    return YES;
} 

Then in your ViewControllers do this:

.h:

@property (strong, nonatomic) ADBannerView *UIiAD;

.m:

- (AppDelegate *) appdelegate {
    return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}

- (void) viewWillAppear:(BOOL)animated {
    UIiAD = [[self appdelegate] UIiAD];
    UIiAD.delegate=self;
   // CODE to correct the layout
}

- (void) viewWillDisappear:(BOOL)animated{
    UIiAD.delegate=nil;
    UIiAD=nil;
    [UIiAD removeFromSuperview];
}

Do this for all the view controllers with appropriate code to redesign the layout!

mrgonuts

hi this looks like a good answer but don't you have to import the AppDelegate in your m files

#import "AppDelegate.h" 

and shouldn't you hide the add if no network connection or add not showing

In the AppDelegate.h:

#import <iAd/iAd.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    ADBannerView *_bannerView;
...
...
}

@property (nonatomic, retain) ADBannerView *_bannerView;

In the AppDelegate.m:

@synthesize _bannerView;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
    if ([ADBannerView instancesRespondToSelector:@selector(initWithAdType:)]) {
        self._bannerView = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
    } else {
        self._bannerView = [[ADBannerView alloc] init];
    }
...
}

In the view controllers that you need to add iAd, create a container UIView with name 'vwAd' and make the connection in xib file where you want to display iAds.

@interface ViewController : UIViewController<ADBannerViewDelegate>
{
    IBOutlet UIView *vwAd;
...
...
}

- (void)layoutAnimated:(BOOL)animated
{
    CGRect contentFrame = self.view.bounds;
    if (contentFrame.size.width < contentFrame.size.height) {
        self.appDelegate._bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
    } else {
        self.appDelegate._bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
    }

    CGRect bannerFrame = self.appDelegate._bannerView.frame;
    if (self.appDelegate._bannerView.bannerLoaded) {
        bannerFrame.origin.y = 0;
    } else {
        bannerFrame.origin.y = vwAd.frame.size.height;
    }

    [UIView animateWithDuration:animated ? 0.25 : 0.0 animations:^{
        self.appDelegate._bannerView.frame = bannerFrame;
    }];
}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    ...

    [self.appDelegate._bannerView removeFromSuperview];
    self.appDelegate._bannerView.delegate = nil;
    self.appDelegate._bannerView.delegate = self;
    [vwAd addSubview:self.appDelegate._bannerView];
    [self layoutAnimated:NO];
}

Please also review iAdSuite examples from Apple. Original layoutAnimated function can be found in iAdSuite examples. Do not forget to add the delegate functions from iAdSuite example into your view controller.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!