-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAppDelegate+BS.m
205 lines (166 loc) · 7.13 KB
/
AppDelegate+BS.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
//
// AppDelegate+BS.m
// FCXNews
//
// Created by 冯 传祥 on 16/5/21.
// Copyright © 2016年 冯 传祥. All rights reserved.
//
#import "AppDelegate+BS.h"
#import <objc/runtime.h>
#import "FCXGuide.h"
#import "FCXOnlineConfig.h"
@implementation AppDelegate (BS)
+ (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 setupBaiduSplash];
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 setupBaiduSplash];
}
}
[self fcx_applicationWillEnterForeground:application];
}
- (void)setupBaiduSplash {
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-02-29"] == NSOrderedDescending);
}
if (!showSplash) {
[FCXGuide startGuide];
return;
}
BaiduMobAdSplash *splash = [[BaiduMobAdSplash alloc] init];
splash.delegate = self;
//把在mssp.baidu.com上创建后获得的代码位id写到这里
splash.AdUnitTag = [FCXOnlineConfig fcxGetConfigParams:@"BD_AdID" defaultValue:@"2058492"];
splash.canSplashClick = YES;
self.splash = splash;
// NSLog(@"bouds %@", NSStringFromCGRect(self.window.bounds));
self.customSplashView = [[UIImageView alloc]initWithFrame:self.window.bounds];
self.customSplashView.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"]];
break;
}
}
self.customSplashView.backgroundColor = [UIColor whiteColor];
[self.window.rootViewController.view addSubview:self.customSplashView];
UIView * baiduSplashContainer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, winSize.width, winSize.height-120)];
[self.customSplashView addSubview:baiduSplashContainer];
[splash loadAndDisplayUsingContainerView:baiduSplashContainer];
UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
closeBtn.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 75, 20, 60, 30);
closeBtn.tag = 100;
[closeBtn setTitle:@"跳过" forState:UIControlStateNormal];
[closeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
closeBtn.titleLabel.font = [UIFont systemFontOfSize:13];
closeBtn.backgroundColor = [UIColor colorWithWhite:0 alpha:.6];
closeBtn.layer.cornerRadius = 2;
[self.customSplashView addSubview:closeBtn];
closeBtn.hidden = YES;
[closeBtn addTarget:self action:@selector(clearSplashData) forControlEvents:UIControlEventTouchUpInside];
}
- (NSString *)publisherId
{
return [FCXOnlineConfig fcxGetConfigParams:@"BD_AppID" defaultValue:@"ccb60059"];
}
/**
* 广告展示成功
*/
- (void)splashSuccessPresentScreen:(BaiduMobAdSplash *)splash
{
NSLog(@"splashSuccessPresentScreen");
UIButton *btn = [self.customSplashView viewWithTag:100];
btn.hidden = NO;
[self.customSplashView bringSubviewToFront:btn];
}
/**
* 广告展示失败
*/
- (void)splashlFailPresentScreen:(BaiduMobAdSplash *)splash withError:(BaiduMobFailReason) reason
{
NSLog(@"splashlFailPresentScreen withError:%d",reason);
//自定义开屏移除
[self clearSplashData];
}
/**
* 广告展示结束
*/
- (void)splashDidDismissScreen:(BaiduMobAdSplash *)splash
{
NSLog(@"splashDidDismissScreen");
//自定义开屏移除
[self clearSplashData];
}
/**
* 广告点击
*/
- (void)splashDidClicked:(BaiduMobAdSplash *)splash
{
NSLog(@"splashDidClicked");
}
- (void)clearSplashData {
[self.customSplashView removeFromSuperview];
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);
}
}
- (BaiduMobAdSplash *)splash {
return objc_getAssociatedObject(self, _cmd);
}
- (void)setSplash:(BaiduMobAdSplash *)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);
}
- (NSDate *)enterBackgroundDate {
return objc_getAssociatedObject(self, _cmd);
}
- (void)setEnterBackgroundDate:(NSDate *)enterBackgroundDate {
objc_setAssociatedObject(self, @selector(enterBackgroundDate), enterBackgroundDate, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end