Single AdMod instance in all ViewControllers

I want to use AdMob in my application, and I have 4 viewControllers (in 4 tabs) where I want to make it visible. As the sample shows, each ViewController must make an instance of the instance and add it as a preview.

I still think that there can be some way to use only one instance that can be used in all applications. How can i do this?

If I create 1 instance in AppDelegate as auto-advertisement and save it in each viewController and in viewDidUnload release it, and then to the next tab element (viewController), I will save it again, etc ... is this a good approach?

+6
source share
3 answers

AppDelegate aproach is a good approach, but you should not save the admob instance in each view controller and release the same in the viewDidUnload method. Instead, simply add the admob view to each viewcontroller viewDidLoad method as a preview. Thus, in AppDelegate only one instance of the Admob view will be displayed.

Hope this helps you.

+2
source

Of course, this will work, but the only problem is that when updating a delegate for an announcement, he will not actually take a new delegate for the announcement unless you explicitly make a new request for the announcement, so your old views will receive any notifications from the announcement. I would recommend using a single-mode adMob, which then forwards any delegate notifications to the correct view.

So, create a class called GADMasterViewController (make sure it also follows the GADBannerViewDelegate protocol) or something that has a static initializer:

 +(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 one 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 that sets the new adView as 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 redirect any notifications you receive to the correct viewController, so one example:

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

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

 - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; shared = [GADMasterViewController singleton]; [shared resetAdView:self]; } 
+7
source

You can declare your admob view in appDelegate and add it as a subview to the window. Access admob view from VC via appDelegate

+3
source

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


All Articles