Rotate iPad and AdMob to not load again, like iAds

☆樱花仙子☆ 提交于 2019-12-01 12:15:55

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