I have implemented AdMob & everything seems to work,
But i wonderd, how can i put the banner in all of my view controllers?
For now, i have the banner only on the RootViewController.
I have total of 4 view controllers.
Thanks.
2 Answers
What you want here is a GADBannerView
singleton of sorts. You can probably create a wrapping class to act as a singleton for your adView, so something like:
@interface GADMasterViewController : UIViewController {
GADBannerView *adBanner_;
BOOL didCloseWebsiteView_;
BOOL isLoaded_;
id currentDelegate_;
}
And just make sure that GADMasterViewController
always returns a singleton:
+(GADMasterViewController *)singleton {
static dispatch_once_t pred;
static GADMasterViewController *shared;
// Will only be run once, the first time this is called
dispatch_once(&pred, ^{
shared = [[GADMasterViewController alloc] init];
});
return shared;
}
Have a method which resets the current view controller that's holding on to the adView:
-(void)resetAdView:(UIViewController *)rootViewController {
// Always keep track of currentDelegate for notification forwarding
currentDelegate_ = rootViewController;
// Ad already requested, simply add it into the view
if (isLoaded_) {
[rootViewController.view addSubview:adBanner_];
} else {
adBanner_.delegate = self;
adBanner_.rootViewController = rootViewController;
adBanner_.adUnitID = kSampleAdUnitID;
GADRequest *request = [GADRequest request];
[adBanner_ loadRequest:request];
[rootViewController.view addSubview:adBanner_];
isLoaded_ = YES;
}
}
Then displaying your ad is just a matter of:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
shared = [GADMasterViewController singleton];
[shared resetAdView:self];
}
You probably need to set up a delegate to forward notifications as well since the AdMob SDK doesn't act well to delegates changing on it in the middle of a request.
You can find a blog post about this here.

- 2,281
- 1
- 16
- 9
-
Thanks RajPara, i tried to follow this post yesturday but " i got lost" and didn't succedded to do that :\... new in all this stuff – Maor Zohar Apr 11 '12 at 04:23
-
Can you provide some more detail about where you got lost? If you have specific questions can definitely try to help you out with them – RajPara Apr 11 '12 at 17:54
I don't know how adMob works but like everything else you can create a BaseViewController
in which you can add your adMob(in the viewDidLoad
method) and then all the other viewControllers can subClass this BaseViewController
. just call [super viewDidLoad];
in the viewDidLoad
methods of your viewControllers and you will have it...
hoping this sorts your problem... :)

- 12,347
- 11
- 63
- 115
-
Thanks Ankit, So let me understand... If i have this in the viewDidLoad of the RootViewController: CGRect frame = CGRectMake(0.0, self.view.frame.size.height, GAD_SIZE_320x50.width, GAD_SIZE_320x50.height); self.adBanner = [[GADBannerView alloc] initWithFrame:frame]; So how do i write what you've said in every viewDidLoad for every VC? Sorry, it just that i'm new in all of this stuff... – Maor Zohar Apr 10 '12 at 16:33
-
everyViewController will subclass rootViewController and then just call [super viewDidLoad]; in the viewDidLoad methods of your viewControllers and you will have it... – Ankit Srivastava Apr 10 '12 at 16:36