I am implementing iAd and AdMob banners into my app. On the iPad I'm getting some weird issues when the device rotates, specifically with AdMob.
With iAds, the banner remains on the bottom of the screen when the device rotates and doesn't reload the ad.
With AdMob however, it reloads the banner when the device rotates, even though I'm using the same code.
I am creating the ADBannerView and GADBannerView programmatically.
iAd code:
self.adBanner.hidden = NO;
self.adBanner = [[self appdelegate] adBanners];
self.adBanner.delegate = self;
if (IDIOM == IPAD)
{
NSLog(@"***This is the iPad****");
[self.adBanner setFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height-80, 320, 50)];
[self.adBanner setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:self.adBanner];
NSLayoutConstraint *myConstraint =[NSLayoutConstraint
constraintWithItem:self.adBanner
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0];
[self.view addConstraint:myConstraint];
myConstraint =[NSLayoutConstraint constraintWithItem:self.adBanner
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:0];
[self.view addConstraint:myConstraint];
myConstraint =[NSLayoutConstraint constraintWithItem:self.adBanner
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1
constant:0];
[self.view addConstraint:myConstraint];
}
The AdMob code is below. I am creating the GADBannerView in the AppDelegate in the applicationDidFinishLaunchingWithOptions:
Update
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// other code
self.adBanners = [[ADBannerView alloc]init];
self.adBanners.hidden = YES;
self.adMobBanners = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait];
return YES;
}
In the View Controller, when I'm creating the AdMob, I am calling the method to create the AdMob:
Update
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"IAPSuccessful"])
{
NSLog(@"View will appear and the IAP is not Successful");
[self sharedBanners];
}
else
{
NSLog(@"View will appear and the IAP IS Successful");
self.adBanner.hidden = YES;
self.adMobBannerView.hidden = YES;
}
}
- (void)sharedBanners
{
self.adMobBannerView = [[self appdelegate] adMobBanners];
self.adMobBannerView.rootViewController = self;
self.adMobBannerView.delegate = self;
self.adBanner = [[self appdelegate] adBanners];
self.adBanner.delegate = self;
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
[self displayiAdsOrNot];
}
- (void)adViewDidReceiveAd:(GADBannerView *)view
{
[self displayAdMobBannerOrNot];
}
- (CustomAppDelegate *)appdelegate
{
return (CustomAppDelegate *)[[UIApplication sharedApplication] delegate];
}
- (void)displayAdMobBannerOrNot
{
self.adBanner.hidden = YES;
self.adMobBannerView.hidden = NO;
self.adMobBannerView = [[self appdelegate] adMobBanners];
self.adMobBannerView.rootViewController = self;
self.adMobBannerView.delegate = self;
if (IDIOM == IPAD) {
[self.adMobBannerView setFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height-80, 320, 50)];
self.adMobBannerView.adUnitID = @"MYUNIT";
// [self.adMobBannerView setFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height-80, CGSizeFromGADAdSize(kGADAdSizeFullBanner).width, 50)];
GADRequest *request = [GADRequest request];
[self.adMobBannerView loadRequest:request];
[self.adMobBannerView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:self.adMobBannerView];
NSLayoutConstraint *myConstraint =[NSLayoutConstraint
constraintWithItem:self.adMobBannerView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0];
[self.view addConstraint:myConstraint];
myConstraint =[NSLayoutConstraint constraintWithItem:self.adMobBannerView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:0];
[self.view addConstraint:myConstraint];
myConstraint =[NSLayoutConstraint constraintWithItem:self.adMobBannerView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1
constant:0];
[self.view addConstraint:myConstraint];
}
}
**Please note, this is not the order of the methods. The actual order is the AppDelegate, the sharedBanner, the displayAdMob, the viewWillAppear and then the delegate methods. **
The reason for the constraints is that I want to have the ADBannerView and GADBannerView pinned to the bottom of the screen and trailing and left. By this I mean, I want it across the bottom of the screen starting at the left edge, ending at the right edge and across the bottom.
Issue
When the iAd banner loads, it works across the entire bottom of the iPad screen, starting at the left and finishing at the right. If I rotate the device, the iAd banner does not reload and it continues to rotate along with the iPad. However, the AdMob banner displays in portrait mode, but when I rotate, it disappears and then reloads.
I have tried using Banner Ad Customization for the constants instead of explicit sizes for the AdMob banner. For example:
if (UIInterfaceOrientationLandscapeLeft)
{
NSLog(@"Left");
[self.adMobBannerView setFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height-80, CGSizeFromGADAdSize(kGADAdSizeSmartBannerLandscape).width, 90)];
}
else if (UIInterfaceOrientationLandscapeRight)
{
NSLog(@"Right");
[self.adMobBannerView setFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height-80, CGSizeFromGADAdSize(kGADAdSizeSmartBannerLandscape).width, 90)];
}
else if (UIInterfaceOrientationPortrait)
{
NSLog(@"Portraait");
[self.adMobBannerView setFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height-80, CGSizeFromGADAdSize(kGADAdSizeSmartBannerPortrait).width, 90)];
}
But the issue is still present.