需求:需要在用戶進(jìn)入應(yīng)用時(shí),彈出全屏廣告,并且使用ADMob。
問(wèn)題:現(xiàn)在的問(wèn)題是ADMob并沒(méi)有開屏廣告,只有插屏廣告,如果等用戶進(jìn)入界面 后再?gòu)棾霾迤翉V告,在谷歌眼里,無(wú)意間彈出廣告是違規(guī)的!
方案:將插屏廣告做成開屏廣告
一.創(chuàng)建一個(gè)對(duì)象和工程名同名
創(chuàng)建對(duì)象
二.貼代碼,備注很詳細(xì)
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 方法中,啟動(dòng)監(jiān)聽,可以做到無(wú)注入
+ (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) {
///如果是沒(méi)啥經(jīng)驗(yàn)的開發(fā),請(qǐng)不要在初始化的代碼里面做別的事,防止對(duì)主線程的卡頓,和 其他情況
///應(yīng)用啟動(dòng), 首次開屏廣告
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidFinishLaunchingNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
///要等DidFinished方法結(jié)束后才能初始化UIWindow,不然會(huì)檢測(cè)是否有rootViewController
[self show];
[self CheakAd];
}];
///進(jìn)入后臺(tái)
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidEnterBackgroundNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
}];
///后臺(tái)啟動(dòng),二次開屏廣告
[[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) {//后臺(tái)控制是否顯示廣告
if([[NSUserDefaults standardUserDefaults] objectForKey:@"admob_pid_chaping"]!=nil){//是否從后臺(tái)獲取到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{
///初始化一個(gè)Window, 做到對(duì)業(yè)務(wù)視圖無(wú)干擾。
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];
///設(shè)置為最頂層,防止 AlertView 等彈窗的覆蓋
window.windowLevel = UIWindowLevelStatusBar + 1;
///默認(rèn)為YES,當(dāng)你設(shè)置為NO時(shí),這個(gè)Window就會(huì)顯示了
window.hidden = NO;
window.alpha = 1;
///防止釋放,顯示完后 要手動(dòng)設(shè)置為 nil
self.window = window;
}
- (void)hide{
///來(lái)個(gè)漸顯動(dòng)畫
[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];
//和啟動(dòng)圖一樣,給用戶造成錯(cuò)覺(jué)
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{//插屏廣告請(qǐng)求失敗
[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
三.其他注意事項(xiàng)
1.一般第一次啟動(dòng)都無(wú)法請(qǐng)求到廣告。
2.你需要特別注意didFinishLaunchingWithOptions里配置好谷歌廣告,不然沒(méi)法正常顯示:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//設(shè)置tabBar的字體顏色
[self setTabBarItemFontColor];
//請(qǐng)求數(shù)據(jù)
[self loadUserData];
//配置
[self Configuration];
//谷歌廣告
[self loadGoogleAd];
//網(wǎng)絡(luò)監(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這個(gè)方法大家最好不要用,有點(diǎn)誤導(dǎo)你們了,這個(gè)是我自己的需求,你們用的時(shí)候最好把這個(gè)函數(shù)刪掉!不然會(huì)出現(xiàn)廣告閃一下就消失的問(wèn)題,我自己是在其他地方做了處理的,這個(gè)地方?jīng)]有貼出來(lái)!
4.如果你看見的效果是先進(jìn)入根視圖,再出現(xiàn)廣告,那很有可能是你少了一張偽裝的啟動(dòng)圖,就是把最大的啟動(dòng)圖重新命名成ADImage.png,并且拖入工程中,不是放在LaunchImage中哦!(名字也不一定叫ADImage啦,你隨意,但是代碼里面的名字也要記得改!)