旋转iPad和AdMob的不再次加载,如网络成瘾者 [英] Rotate iPad and AdMob to not load again, like iAds

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

问题描述

我采取的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.

使用成瘾者中,横幅保留在屏幕的底部时,该设备旋转,并且不重新加载广告。

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

使用的AdMob但是,重新加载的旗帜时,该设备旋转,即使我用同样的code。

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

我创建了 ADBannerView GADBannerView 编程。

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

}

在AdMob的code是如下。我创建了GADBannerView在AppDelegate中的applicationDidFinishLaunchingWithOptions:

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

原因的限制是,我想有 ADBannerView GADBannerView 寄托的底部屏幕和尾随离开。我的意思是,我希望它在屏幕的底部开始的左边缘,右边缘,并在底部结束。

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屏幕的整个底部,从左边开始,并在适当的整理。如果我旋转设备,网络成瘾的旗帜不会重新加载并继续与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)];
        }

但问题仍然是present。

But the issue is still present.

推荐答案

更​​新的问题已更新的答案:

Updated answer for updated question:

所以,有很多与你的code回事。你似乎要创建在多个属性都你的的AppDelegate 的ViewController ,并重复一些相同的code 。我已经先行一步,并清理并重新实现了两个内部审计部门和AdMob完全共享的横幅。我没有遇到AdMob的旗帜问题,旋转设备时。这code青睐的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的不再次加载,如网络成瘾者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆