问题
I am using Google AdMob for iOS:
Google AdMob
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);
}
}
回答1:
Because the class implementation was actually a plugin I was able to use the following code:
for (UIView *subview in [self.viewController.view subviews]) {
if([subview isKindOfClass:[GADBannerView class]]) {
[subview removeFromSuperview];
}
}
According to Phonegap documentation, every plugin has a self.viewController
property. So it was just a matter of looping through and removing only the GADBannerView
from the superview.
Of course I had to #import "GADBannerView.h"
in the plugin class implementation first so it knew about GADBannerView
.
回答2:
without any experience with admob I would say just disable the BannerView and any controller
like bannerView = nil
or [bannerView release]
also [bannerView removeFromSuperview]
or bannerView.hidden = YES
from your own answer, and your added code, all you need to do is
-(void)disableAds
{
// Turn the ads off.
[bannerView_ removeFromSuperview];
}
回答3:
try this: bannerView.rootViewController = nil;
来源:https://stackoverflow.com/questions/12313219/programmatically-turn-off-google-admob-ads-ios