旋转 iPad 和 AdMob 使其不再加载,如 iAds

Rotate iPad and AdMob to not load again, like iAds(旋转 iPad 和 AdMob 使其不再加载,如 iAds)

本文介绍了旋转 iPad 和 AdMob 使其不再加载,如 iAds的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

With iAds, the banner remains on the bottom of the screen when the device rotates and doesn't reload the ad.

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

With AdMob however, it reloads the banner when the device rotates, even though I'm using the same code.

我正在以编程方式创建 ADBannerViewGADBannerView.

I am creating the ADBannerView and GADBannerView programmatically.

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:

The AdMob code is below. I am creating the GADBannerView in the AppDelegate in the applicationDidFinishLaunchingWithOptions:

更新

- (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 的方法:

In the View Controller, when I'm creating the AdMob, I am calling the method to create the 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,然后是委托方法.**

**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. **

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

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.

问题

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

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.

我已尝试使用 横幅广告自定义 用于常量而不是 AdMob 横幅的明确尺寸.例如:

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

但问题仍然存在.

推荐答案

更新问题的更新答案:

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

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

这篇关于旋转 iPad 和 AdMob 使其不再加载,如 iAds的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:旋转 iPad 和 AdMob 使其不再加载,如 iAds

基础教程推荐