I am using Google AdMob for iOS:
I was wondering whether I’m able to turn off these ads programmatically so they stop displaying. After reading through the SDK I can’t see anywhere to toggle the ads on or off.
EDIT:
This is how I load the Google AdMob code:
MainViewController.m
- (void) viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// Create a view of the standard size at the bottom of the screen.
// Available AdSize constants are explained in GADAdSize.h.
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
// Must be a better way to position at bottom of page
[bannerView_ setCenter:CGPointMake(kGADAdSizeBanner.size.width/2, 455)];
// Specify the ad's "unit identifier." This is your AdMob Publisher ID.
bannerView_.adUnitID = MY_BANNER_UNIT_ID;
// Let the runtime know which UIViewController to restore after taking
// the user wherever the ad goes and add it to the view hierarchy.
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
// Initiate a generic request to load it with an ad.
GADRequest *request = [GADRequest request];
// remove this line when you are ready to deploy for real
request.testing = YES;
[bannerView_ loadRequest:request];
}
I’d like to disable the superview within a class implementation:
This is the code I’ve tried so far to loop through the MainViewController subviews.
Once I’ve found the right subview GADBannerView I want to be able to remove it.
OtherClass.m
- (void)disableAds
{
// Turn the ads off.
UIViewController *mainView = [[UIViewController alloc] initWithNibName:@"MainViewController" bundle:[NSBundle mainBundle]];
for (UIView *subview in [mainView.view subviews]) {
NSLog(@"View(s): %@", subview);
}
}
Because the class implementation was actually a plugin I was able to use the following code:
According to Phonegap documentation, every plugin has a
self.viewControllerproperty. So it was just a matter of looping through and removing only theGADBannerViewfrom the superview.Of course I had to
#import "GADBannerView.h"in the plugin class implementation first so it knew aboutGADBannerView.