iOS开发 如何将ADMob的插屏广告做成开屏广告

需求:需要在用户进入应用时,弹出全屏广告,并且使用ADMob。
问题:现在的问题是ADMob并没有开屏广告,只有插屏广告,如果等用户进入界面 后再弹出插屏广告,在谷歌眼里,无意间弹出广告是违规的!
方案:将插屏广告做成开屏广告
参考:《iOS-优雅的插入开屏广告》-- 不改动任何一行代码

一.创建一个对象和工程名同名
iOS开发 如何将ADMob的插屏广告做成开屏广告_第1张图片
创建对象.png
二.贴代码,备注很详细
XYRPlayer.h
#import 

@interface XYRPlayer : NSObject

@end
XYRPlayer.m
#import "XYRPlayer.h"
#import 

@import GoogleMobileAds;
@interface XYRPlayer(){
    
    UIViewController *AdViewController;
}

@property (nonatomic, strong) UIWindow* window;

@property(nonatomic, strong) GADInterstitial *interstitial;

@end

@implementation XYRPlayer

//在load 方法中,启动监听,可以做到无注入
+ (void)load{
    [self shareInstance];
}

+ (instancetype)shareInstance{
    static id instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

- (instancetype)init{
    self = [super init];
    if (self) {
        
        ///如果是没啥经验的开发,请不要在初始化的代码里面做别的事,防止对主线程的卡顿,和 其他情况
        ///应用启动, 首次开屏广告
        [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidFinishLaunchingNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
            ///要等DidFinished方法结束后才能初始化UIWindow,不然会检测是否有rootViewController
            [self show];
            [self CheakAd];
        }];
        ///进入后台
        [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidEnterBackgroundNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
            
        }];
        ///后台启动,二次开屏广告
        [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationWillEnterForegroundNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
            [self show];
            [self CheakAd];
        }];
    }
    return self;
}



-(void)CheakAd{//这一部分的逻辑大家根据自身需求定制
    //谷歌插屏广告
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"adshow"] intValue]!=0) {//后台控制是否显示广告
        if([[NSUserDefaults standardUserDefaults] objectForKey:@"admob_pid_chaping"]!=nil){//是否从后台获取到pid
            if(![[NSUserDefaults standardUserDefaults] boolForKey:@"VIPUser"]){//是否是工作人员,工作人员免广告
                NSArray *chapingArr=[[NSUserDefaults standardUserDefaults] objectForKey:@"admob_pid_chaping"];
                self.interstitial = [[GADInterstitial alloc] initWithAdUnitID:chapingArr[0]];
                self.interstitial.delegate=self;
                GADRequest *request = [GADRequest request];
                [self.interstitial loadRequest:request];
            }else{
                [self hide];
            }
        }else{
            [self hide];
        }
    }else{
        [self hide];
    }
}

- (void)show{
    ///初始化一个Window, 做到对业务视图无干扰。
    UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    AdViewController=[UIViewController new];
    window.rootViewController = AdViewController;
    window.rootViewController.view.backgroundColor = [UIColor clearColor];
    window.rootViewController.view.userInteractionEnabled = NO;
    
    ///广告布局
    [self setupSubviews:window];
    
    ///设置为最顶层,防止 AlertView 等弹窗的覆盖
    window.windowLevel = UIWindowLevelStatusBar + 1;
    
    ///默认为YES,当你设置为NO时,这个Window就会显示了
    window.hidden = NO;
    window.alpha = 1;
    
    ///防止释放,显示完后  要手动设置为 nil
    self.window = window;
}

- (void)hide{
    ///来个渐显动画
    [UIView animateWithDuration:0.3 animations:^{
        self.window.alpha = 0;
    } completion:^(BOOL finished) {
        [self.window.subviews.copy enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [obj removeFromSuperview];
        }];
        self.window.hidden = YES;
        self.window = nil;
    }];
}

///初始化显示的视图, 可以挪到具
- (void)setupSubviews:(UIWindow*)window{
    ///随便写写
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:window.bounds];
    //和启动图一样,给用户造成错觉
    imageView.image = [UIImage imageNamed:@"ADImage.png"];
    imageView.contentMode=UIViewContentModeScaleAspectFill;
    
    [window addSubview:imageView];
}

#pragma mark -GADInterstitialDelegate
- (void)interstitialDidReceiveAd:(GADInterstitial *)ad{//接收到插屏广告
    [self.interstitial presentFromRootViewController:AdViewController];
}

- (void)interstitial:(GADInterstitial *)ad didFailToReceiveAdWithError:(GADRequestError *)error{//插屏广告请求失败
    [self hide];
}

/**********************/
- (void)interstitialWillPresentScreen:(GADInterstitial *)ad{
    //插屏广告即将开始
    NSLog(@"插屏广告即将开始");
}

- (void)interstitialDidFailToPresentScreen:(GADInterstitial *)ad{
    //插屏广告失败
    NSLog(@"插屏广告失败");
}

- (void)interstitialWillDismissScreen:(GADInterstitial *)ad{
    //插屏广告即将消失
    NSLog(@"插屏广告即将消失");
    [self hide];
}

- (void)interstitialDidDismissScreen:(GADInterstitial *)ad{
    //插屏广告已经消失
    NSLog(@"插屏广告已经消失");
}

- (void)interstitialWillLeaveApplication:(GADInterstitial *)ad{
    //插屏广告即将离开APP
    NSLog(@"插屏广告即将离开APP");
}

@end
三.其他注意事项

1.一般第一次启动都无法请求到广告。

2.你需要特别注意didFinishLaunchingWithOptions里配置好谷歌广告,不然没法正常显示:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //设置tabBar的字体颜色
    [self setTabBarItemFontColor];

    //请求数据
    [self loadUserData];
    
    //配置
    [self Configuration];
    
    //谷歌广告
    [self loadGoogleAd];
    
    //网络监控
    [self netWorkChangeEvent];
    
    return YES;
}

-(void)loadGoogleAd{
    // Use Firebase library to configure APIs
    [FIRApp configure];
    // Initialize Google Mobile Ads SDK
    [GADMobileAds configureWithApplicationID:@"你的admob_appid"];
}

3.CheakAd这个方法大家最好不要用,有点误导你们了,这个是我自己的需求,你们用的时候最好把这个函数删掉!不然会出现广告闪一下就消失的问题,我自己是在其他地方做了处理的,这个地方没有贴出来!

4.如果你看见的效果是先进入根视图,再出现广告,那很有可能是你少了一张伪装的启动图,就是把最大的启动图重新命名成ADImage.png,并且拖入工程中,不是放在LaunchImage中哦!(名字也不一定叫ADImage啦,你随意,但是代码里面的名字也要记得改!)

你可能感兴趣的:(iOS开发 如何将ADMob的插屏广告做成开屏广告)