旋转 iPad 和 AdMob 不再加载,就像 iAds

Rotate iPad and AdMob to not load again, like iAds

我正在我的应用程序中实施 iAd 和 AdMob 横幅广告。在 iPad 上,当设备旋转时,我遇到了一些奇怪的问题,特别是 AdMob。

使用 iAds 时,横幅会在设备旋转时保留在屏幕底部,不会重新加载广告。

然而,使用 AdMob,它会在设备旋转时重新加载横幅,即使我使用的是相同的代码。

我正在以编程方式创建 ADBannerViewGADBannerView

iAd代码:

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];

}

AdMob 代码如下。我正在 applicationDidFinishLaunchingWithOptions:

的 AppDelegate 中创建 GADBannerView

更新

- (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; 
}

在视图控制器中,当我创建 AdMob 时,我调用了创建 AdMob 的方法:

更新

- (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];
    }
}

**请注意,这不是方法的顺序。实际顺序是 AppDelegate、sharedBanner、displayAdMob、viewWillAppear,然后是委托方法。 **

限制的原因是我想将 ADBannerViewGADBannerView 固定到屏幕底部并尾随左侧。我的意思是,我希望它在屏幕底部从左边缘开始,在右边缘结束并穿过底部。

问题

加载 iAd 横幅时,它会在 iPad 屏幕的整个底部显示,从左侧开始到右侧结束。如果我旋转设备,iAd 横幅不会重新加载,它会继续与 iPad 一起旋转。但是,AdMob 横幅以纵向模式显示,但当我旋转时,它消失然后重新加载。

我尝试使用 Banner Ad Customization 作为常量,而不是 AdMob 横幅的明确尺寸。例如:

        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)];
        }

但问题依然存在。

更新问题的更新答案:

所以您的代码有很多问题。您似乎在 AppDelegateViewController 中创建了多个属性,并重复了一些相同的代码。我已经继续清理并完全重新实现了 iAd 和 AdMob 共享横幅。旋转设备时,我没有遇到 AdMob 横幅问题。此代码有利于 iAd,并且仅在 iAd 无法加载广告时显示 AdMob 横幅。试一试,如果您有任何问题,请告诉我。

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];
}