Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AdMod single instance in all ViewControllers

I want to use AdMob in my app and I have 4 viewControllers (in 4 tab items) where I want to make it visible. The way the sample shows, every ViewController needs to make instance of it and add as subview.

I am still thinking if there can be someway to use only 1 instance that can be used in all application. How can I do that?

If I create 1 instance in AppDelegate as autorelease and retain in each viewController and on viewDidUnload release it and then in next tab item (viewController) i again retain it etc...is it good approach?

like image 723
Anand Avatar asked Mar 14 '12 09:03

Anand


2 Answers

Sure, that would work but the only problem is that when you update the delegate for the ad, it will not actually pick up the new delegate for the ad unless you explicitly make a new ad request, so your old views will be receiving any notifications from the ad. I'd recommend the approach of making an adMob singleton that then forwards any delegate notifications to the right view.

So creating a class called GADMasterViewController (make sure it follows the GADBannerViewDelegate protocol too) or something which has a static initializer as so:

+(GADMasterViewController *)singleton {
  static dispatch_once_t pred;
  static GADMasterViewController *shared = nil;
  dispatch_once(&pred, ^{
    shared = [[GADMasterViewController alloc] init];
  });
  return shared;
}

Then in the initializer you can initialize a single GADBannerView as a property of this singleton:

-(id)init
{
  if (self = [super init])
  {
    self.adBanner = [[GADBannerView alloc]
                     initWithFrame:CGRectMake(0.0,
                                              0.0,
                                              GAD_SIZE_320x50.width,
                                              GAD_SIZE_320x50.height)];

    // Has an ad request already been made
    self.isLoaded = NO;
  }
  return self;
}

Then you can have a method which sets your new adView as the currentDelegate as such:

-(void)resetAdView:(UIViewController<GADBannerViewDelegate> *)rootViewController {

  if (self.isLoaded) {
     currentDelegate_ = rootViewController;
    [rootViewController.view addSubview:self.adBanner];
  }
  else {
    // The delegate to forward any notifications too
    currentDelegate_ = rootViewController;

    self.adBanner.delegate = self;
    self.adBanner.rootViewController = rootViewController;
    self.adBanner.adUnitID = kSampleAdUnitID;

    GADRequest *request = [GADRequest request];

    [self.adBanner loadRequest:request];
    [rootViewController.view addSubview:self.adBanner];
    self.isLoaded = YES;
  }
}

At this point, you just want to forward any notifications which you get to the right viewController, so one example would be:

- (void)adViewDidReceiveAd:(GADBannerView *)view {
  if ([currentDelegate_ respondsToSelector:@selector(adViewDidReceiveAd:)]) {
    [currentDelegate_ adViewDidReceiveAd:view];
  }
}

In ViewControllerX (one of your 4 ViewControllers), you could just add it to your view hierarchy using:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    shared = [GADMasterViewController singleton];
    [shared resetAdView:self];
}
like image 51
RajPara Avatar answered Sep 30 '22 00:09

RajPara


You can declare your admob view in appDelegate and add it as subview to window. Refer to admob view from VC through appDelegate

like image 28
beryllium Avatar answered Sep 30 '22 01:09

beryllium