-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXXADManager.m
179 lines (146 loc) · 5 KB
/
XXADManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//
// XXADManager.m
// XXAD
//
// Created by SWKJ on 2019/9/24.
// Copyright © 2019 XX. All rights reserved.
//
#import "XXADManager.h"
#import <GoogleMobileAds/GADInterstitialDelegate.h>
@import GoogleMobileAds;
@interface XXADManager() <GADInterstitialDelegate>
{
UIViewController *AdViewController;
}
@property (nonatomic, strong) UIWindow* window;
@property(nonatomic, strong) GADInterstitial *interstitial;
@end
@implementation XXADManager
//在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
{ //这一部分的逻辑大家根据自身需求定制
//谷歌插屏广告
self.interstitial = [self createAndLoadInterstitial];
}
- (GADInterstitial *)createAndLoadInterstitial
{
GADInterstitial *interstitial =
[[GADInterstitial alloc] initWithAdUnitID:@"你的广告ID"];
interstitial.delegate = self;
[interstitial loadRequest:[GADRequest request]];
return interstitial;
}
- (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 createImageWithColor:[UIColor whiteColor]];
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 *)interstitial
{
//插屏广告已经消失
NSLog(@"插屏广告已经消失");
//self.interstitial = [self createAndLoadInterstitial];
}
- (void)interstitialWillLeaveApplication:(GADInterstitial *)ad{
//插屏广告即将离开APP
NSLog(@"插屏广告即将离开APP");
}
@end