iOS開發(fā) 如何將ADMob的插屏廣告做成開屏廣告

需求:需要在用戶進入應用時,彈出全屏廣告,并且使用ADMob。
問題:現(xiàn)在的問題是ADMob并沒有開屏廣告,只有插屏廣告,如果等用戶進入界面 后再彈出插屏廣告,在谷歌眼里,無意間彈出廣告是違規(guī)的!
方案:將插屏廣告做成開屏廣告
參考《iOS-優(yōu)雅的插入開屏廣告》-- 不改動任何一行代碼

一.創(chuàng)建一個對象和工程名同名
創(chuàng)建對象.png
二.貼代碼,備注很詳細
XYRPlayer.h
#import <UIKit/UIKit.h>

@interface XYRPlayer : NSObject

@end
XYRPlayer.m
#import "XYRPlayer.h"
#import <GoogleMobileAds/GADInterstitialDelegate.h>

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

@property (nonatomic, strong) UIWindow* window;

@property(nonatomic, strong) GADInterstitial *interstitial;

@end

@implementation XYRPlayer

//在load 方法中,啟動監(jiān)聽,可以做到無注入
+ (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) {
        
        ///如果是沒啥經(jīng)驗的開發(fā),請不要在初始化的代碼里面做別的事,防止對主線程的卡頓,和 其他情況
        ///應用啟動, 首次開屏廣告
        [[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{//這一部分的邏輯大家根據(jù)自身需求定制
    //谷歌插屏廣告
    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, 做到對業(yè)務視圖無干擾。
    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{
    //插屏廣告已經(jīng)消失
    NSLog(@"插屏廣告已經(jīng)消失");
}

- (void)interstitialWillLeaveApplication:(GADInterstitial *)ad{
    //插屏廣告即將離開APP
    NSLog(@"插屏廣告即將離開APP");
}

@end
三.其他注意事項

1.一般第一次啟動都無法請求到廣告。

2.你需要特別注意didFinishLaunchingWithOptions里配置好谷歌廣告,不然沒法正常顯示:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //設置tabBar的字體顏色
    [self setTabBarItemFontColor];

    //請求數(shù)據(jù)
    [self loadUserData];
    
    //配置
    [self Configuration];
    
    //谷歌廣告
    [self loadGoogleAd];
    
    //網(wǎng)絡監(jiān)控
    [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這個方法大家最好不要用,有點誤導你們了,這個是我自己的需求,你們用的時候最好把這個函數(shù)刪掉!不然會出現(xiàn)廣告閃一下就消失的問題,我自己是在其他地方做了處理的,這個地方?jīng)]有貼出來!

4.如果你看見的效果是先進入根視圖,再出現(xiàn)廣告,那很有可能是你少了一張偽裝的啟動圖,就是把最大的啟動圖重新命名成ADImage.png,并且拖入工程中,不是放在LaunchImage中哦!(名字也不一定叫ADImage啦,你隨意,但是代碼里面的名字也要記得改!)

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,169評論 25 708
  • 【滿庭芳~荷塘】 (一) 文|梅易 翠葉平鋪,紅胭濃抹,荷塘半夏情濃。 輕舟水上,一曲入花叢。 驚起鴛鴦兩處,角亭...
    風云梅易閱讀 1,078評論 2 1
  • 五月的暖陽挺柔和 有時,會碰見沒有純白的云 風輕輕吹起簾子我就能看到一片的綠 這,都是一些很平常的事 列車緩緩駛來...
    七月書生閱讀 201評論 0 0
  • 江南好,奇景嘆無雙,秋水寒碧沉樹影,漏窗明瑟潑茶香,閑坐正微涼。 提起蘇州園林,幾乎無人不驚嘆叫絕。這個著名的歷史...
    萬久閱讀 1,358評論 0 5
  • 李釹詩閱讀 201評論 0 0

友情鏈接更多精彩內容