How to create a global link for iAd and implement it in several Viewcontrollers

I have 5 ViewControllers in each of them that I have to display iAd, so I need to implement iAd code in each ViewControllers. Instead, if I create a set of generic code in AppDelegate, I can just call that code wherever I need an iAd to display.

If someone has implemented this iAd concept, this will help me get out of this problem. Thanks at Advance.

+6
source share
3 answers

Just create a pointer to iAD in the 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 the following:

.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 view controllers with the appropriate code to reverse engineer the layout!

+5
source

Hi, this seems like a good answer, but you don't need to import AppDelegate into your m files.

 #import "AppDelegate.h" 

and do not hide the add if there is no network connection or add do not show

+1
source

In AppDelegate.h:

 #import <iAd/iAd.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> { ADBannerView *_bannerView; ... ... } @property (nonatomic, retain) ADBannerView *_bannerView; 

In 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 UIView container named "vwAd" and create a connection in the 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]; } 

Also check out Apple's iAdSuite examples. The original layout. The designated function can be found in iAdSuite examples. Remember to add the delegate functions from the iAdSuite example to your view controller.

0
source

Source: https://habr.com/ru/post/915200/


All Articles