Rotate iPad and AdMob to not load again, like iAds

主宰稳场 提交于 2019-12-30 12:15:08

问题


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.


回答1:


Updated answer for updated question:

So there's a lot going on with your code. You seem to be creating multiple properties in both your AppDelegate and ViewController, and repeating some of the same code. I've gone ahead and cleaned up and reimplemented both the iAd and AdMob shared banners completely. I am not experiencing the AdMob banner issue when rotating the device. This code favors iAd and only displays an AdMob banner if iAd fails to load an ad. Give it a try and let me know if you have any questions.

AppDelegate.h

#import <UIKit/UIKit.h>
@import iAd; // Import iAd
@import GoogleMobileAds; // Import AdMob

// Include AdMob and iAd delegates
@interface AppDelegate : UIResponder <UIApplicationDelegate, GADBannerViewDelegate, ADBannerViewDelegate>

@property (strong, nonatomic) UIWindow *window;

// Create properties
@property (strong, nonatomic) GADBannerView *adMobBanner;
@property (strong, nonatomic) ADBannerView *iAdBanner;

@end

AppDelegate.m

#import "AppDelegate.h"
@interface AppDelegate ()
@end

@implementation AppDelegate

// Synthesize properties
@synthesize adMobBanner;
@synthesize iAdBanner;

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Create iAd banner
    iAdBanner = [[ADBannerView alloc]init];
    // Create AdMob banner
    adMobBanner = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
    return YES;
}

-(void)bannerViewDidLoadAd:(ADBannerView *)banner{
    // Got ad from iAd
    // Lets show iAd and hide AdMob
    [UIView beginAnimations:nil context:NULL];
    iAdBanner.alpha = 1.0;
    adMobBanner.alpha = 0.0;
    [UIView commitAnimations];
    NSLog(@"iAd loaded ad");
}

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
    // iAd failed to load an ad
    // Lets hide iAd and show AdMob
    [UIView beginAnimations:nil context:NULL];
    iAdBanner.alpha = 0.0;
    adMobBanner.alpha = 1.0;
    [UIView commitAnimations];
    NSLog(@"iAd failed to load ad");
}

ViewController.h

#import <UIKit/UIKit.h>
#import "AppDelegate.h" // Import our AppDelegate header

@interface ViewController : UIViewController {
    AppDelegate *appDelegate;
}

@end

ViewController.m

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    // Create reference to our AppDelegate
    appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    // Now we can access our banners by appDelegate.banner

    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"IAPSuccessful"]) {
        NSLog(@"User has NOT PURCHASED IAP");
        // IAP not purchased
        // Lets setup some ads
        [self setupAds];
    }
    else {
        NSLog(@"User HAS PURCHASED IAP");
        // IAP purchased
        // Lets hide those ads
        appDelegate.iAdBanner.hidden = YES;
        appDelegate.adMobBanner.hidden = YES;
    }
}

-(void)setupAds {
    // AdMob
    appDelegate.adMobBanner.rootViewController = self;
    appDelegate.adMobBanner.delegate = appDelegate;
    GADRequest *request = [GADRequest request];
    appDelegate.adMobBanner.adUnitID = MY_BANNER_UNIT_ID;
    [appDelegate.adMobBanner loadRequest:request];
    [appDelegate.adMobBanner setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.view addSubview:appDelegate.adMobBanner];

    NSLayoutConstraint *myConstraint =[NSLayoutConstraint
                                       constraintWithItem:appDelegate.adMobBanner
                                       attribute:NSLayoutAttributeLeading
                                       relatedBy:NSLayoutRelationEqual
                                       toItem:self.view
                                       attribute:NSLayoutAttributeLeading
                                       multiplier:1.0
                                       constant:0];

    [self.view addConstraint:myConstraint];
    myConstraint =[NSLayoutConstraint constraintWithItem:appDelegate.adMobBanner
                                               attribute:NSLayoutAttributeTrailing
                                               relatedBy:NSLayoutRelationEqual
                                                  toItem:self.view
                                               attribute:NSLayoutAttributeTrailing
                                              multiplier:1
                                                constant:0];

    [self.view addConstraint:myConstraint];
    myConstraint =[NSLayoutConstraint constraintWithItem:appDelegate.adMobBanner
                                               attribute:NSLayoutAttributeBottom
                                               relatedBy:NSLayoutRelationEqual
                                                  toItem:self.view
                                               attribute:NSLayoutAttributeBottom
                                              multiplier:1
                                                constant:0];
    [self.view addConstraint:myConstraint];

    // iAd
    appDelegate.iAdBanner.delegate = appDelegate;
    [appDelegate.iAdBanner setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.view addSubview:appDelegate.iAdBanner];
    appDelegate.iAdBanner.alpha = 0.0;

    myConstraint =[NSLayoutConstraint
                                       constraintWithItem:appDelegate.iAdBanner
                                       attribute:NSLayoutAttributeLeading
                                       relatedBy:NSLayoutRelationEqual
                                       toItem:self.view
                                       attribute:NSLayoutAttributeLeading
                                       multiplier:1.0
                                       constant:0];

    [self.view addConstraint:myConstraint];
    myConstraint =[NSLayoutConstraint constraintWithItem:appDelegate.iAdBanner
                                               attribute:NSLayoutAttributeTrailing
                                               relatedBy:NSLayoutRelationEqual
                                                  toItem:self.view
                                               attribute:NSLayoutAttributeTrailing
                                              multiplier:1
                                                constant:0];

    [self.view addConstraint:myConstraint];
    myConstraint =[NSLayoutConstraint constraintWithItem:appDelegate.iAdBanner
                                               attribute:NSLayoutAttributeBottom
                                               relatedBy:NSLayoutRelationEqual
                                                  toItem:self.view
                                               attribute:NSLayoutAttributeBottom
                                              multiplier:1
                                                constant:0];
    [self.view addConstraint:myConstraint];
}


来源:https://stackoverflow.com/questions/29917987/rotate-ipad-and-admob-to-not-load-again-like-iads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!