-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAppDelegate+HalfGS.m
212 lines (167 loc) · 7.43 KB
/
AppDelegate+HalfGS.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//
// AppDelegate+HalfGS.m
// Finance
//
// Created by 冯 传祥 on 16/4/24.
// Copyright © 2016年 冯 传祥. All rights reserved.
//
#import "AppDelegate+HalfGS.h"
#import <objc/runtime.h>
#import "FCXGuide.h"
#import "FCXOnlineConfig.h"
@implementation AppDelegate (HalfGS)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[self swizzleInstanceMethodWithClass:[self class] originalSelector:@selector(application:didFinishLaunchingWithOptions:) swizzledMethod:@selector(fcx_application:didFinishLaunchingWithOptions:)];
[self swizzleInstanceMethodWithClass:[self class] originalSelector:@selector(applicationDidEnterBackground:) swizzledMethod:@selector(fcx_applicationDidEnterBackground:)];
[self swizzleInstanceMethodWithClass:[self class] originalSelector:@selector(applicationWillEnterForeground:) swizzledMethod:@selector(fcx_applicationWillEnterForeground:)];
});
}
- (BOOL)fcx_application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self setupS];
return [self fcx_application:application didFinishLaunchingWithOptions:launchOptions];
}
//app已经进入后台后
- (void)fcx_applicationDidEnterBackground:(UIApplication *)application {
self.enterBackgroundDate = [NSDate date];
[self fcx_applicationDidEnterBackground:application];
}
//app将要进入前台
- (void)fcx_applicationWillEnterForeground:(UIApplication *)application {
if (self.enterBackgroundDate) {
NSDate *currentDate = [NSDate date];
double duration = [currentDate timeIntervalSinceDate:self.enterBackgroundDate];
if (duration >= 30 * 60) {//超过30分钟再次显示开屏
[self setupS];
}
}
[self fcx_applicationWillEnterForeground:application];
}
- (void)setupS {
BOOL showSplash;
if ([FCXOnlineConfig fcxGetConfigParams:@"showSplash"]) {
showSplash = [[FCXOnlineConfig fcxGetConfigParams:@"showSplash"] boolValue];
}else {//首次进入应用,请求不到友盟的参数,根据日期判断是否显示
NSDate *currentDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat: @"yyyy-MM-dd"];
NSString *currentDateString = [dateFormatter stringFromDate:currentDate];
showSplash = ([currentDateString compare:@"2016-05-10"] == NSOrderedDescending);
}
if (!showSplash) {
[FCXGuide startGuide];
return;
}
NSString *appKey = @"1105280859";
NSString *placementId = @"1010512077974527";
NSString *paramsString = [FCXOnlineConfig fcxGetConfigParams:@"GDT_SplashInfo" defaultValue:@""];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[paramsString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
if([dict isKindOfClass:[NSDictionary class]]){
appKey = dict[@"appkey"];;
placementId = dict[@"placementId"];
}
//开屏广告初始化
self.splash = [[GDTSplashAd alloc] initWithAppkey:appKey placementId:placementId];
self.splash.delegate = self;//设置代理
self.customSplashView = [[UIImageView alloc]initWithFrame:self.window.bounds];
self.customSplashView.userInteractionEnabled = YES;
self.windowCoverView = [[UIImageView alloc] initWithFrame:self.window.bounds];
self.windowCoverView.userInteractionEnabled = YES;
//针对不同设备尺寸设置不同的默认图片,拉取广告等待时间会展示该默认图片。
CGSize winSize = self.window.frame.size;
NSArray* imagesDict = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"];
for (NSDictionary* dict in imagesDict) {
if(CGSizeEqualToSize(CGSizeFromString(dict[@"UILaunchImageSize"]),winSize))
{
self.customSplashView.image = [UIImage imageNamed:dict[@"UILaunchImageName"]];
self.windowCoverView.image = self.customSplashView.image;
self.splash.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:dict[@"UILaunchImageName"]]];
break;
}
}
[self.window.rootViewController.view addSubview:self.customSplashView];
[self.window addSubview:self.windowCoverView];
//设置开屏拉取时长限制,若超时则不再展示广告
self.splash.fetchDelay = 3;
UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 120)];
bottomView.backgroundColor = [UIColor clearColor];
//拉取并展示
[self.splash loadAdAndShowInWindow:self.window withBottomView:bottomView];
}
-(void)splashAdSuccessPresentScreen:(GDTSplashAd *)splashAd
{
DBLOG(@"%s",__FUNCTION__);
}
-(void)splashAdFailToPresent:(GDTSplashAd *)splashAd withError:(NSError *)error
{
DBLOG(@"%s%@",__FUNCTION__,error);
[self clearSplashData];
}
-(void)splashAdClicked:(GDTSplashAd *)splashAd
{
DBLOG(@"%s",__FUNCTION__);
[self clearSplashData];
}
-(void)splashAdWillPresentFullScreenModal:(GDTSplashAd *)splashAd
{
DBLOG(@"%s",__FUNCTION__);
}
-(void)splashAdApplicationWillEnterBackground:(GDTSplashAd *)splashAd
{
DBLOG(@"%s",__FUNCTION__);
}
-(void)splashAdClosed:(GDTSplashAd *)splashAd
{
DBLOG(@"%s",__FUNCTION__);
[self clearSplashData];
}
- (void)splashAdDidDismissFullScreenModal:(GDTSplashAd *)splashAd {
DBLOG(@"%s",__FUNCTION__);
[self clearSplashData];
}
- (void)clearSplashData {
[self.customSplashView removeFromSuperview];
[self.windowCoverView removeFromSuperview];
self.windowCoverView = nil;
self.splash.delegate = nil;
self.splash = nil;
self.customSplashView = nil;
[FCXGuide startGuide];
}
+ (void)swizzleInstanceMethodWithClass:(Class)class
originalSelector:(SEL)originalSelector
swizzledMethod:(SEL)swizzledSelector {
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
if (class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))) {
class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
}else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
- (GDTSplashAd *)splash {
return objc_getAssociatedObject(self, _cmd);
}
- (void)setSplash:(GDTSplashAd *)splash {
objc_setAssociatedObject(self, @selector(splash), splash, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIImageView *)customSplashView {
return objc_getAssociatedObject(self, _cmd);
}
- (void)setCustomSplashView:(UIImageView *)customSplashView {
objc_setAssociatedObject(self, @selector(customSplashView), customSplashView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIImageView *)windowCoverView {
return objc_getAssociatedObject(self, _cmd);
}
- (void)setWindowCoverView:(UIImageView *)windowCoverView {
objc_setAssociatedObject(self, @selector(windowCoverView), windowCoverView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSDate *)enterBackgroundDate {
return objc_getAssociatedObject(self, _cmd);
}
- (void)setEnterBackgroundDate:(NSDate *)enterBackgroundDate {
objc_setAssociatedObject(self, @selector(enterBackgroundDate), enterBackgroundDate, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end