-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFCXRating.m
342 lines (284 loc) · 11.9 KB
/
FCXRating.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//
// FCXRating.h
// Universial
//
// Created by 冯 传祥 on 15/8/23.
// Copyright (c) 2015年 冯 传祥. All rights reserved.
//
#import "FCXRating.h"
#import "FCXGuide.h"
#import "UMFeedback.h"
#import "FCXOnlineConfig.h"
#import "UMMobClick/MobClick.h"
#define HASRATING @"HasRating"
@implementation FCXRating
{
NSString *_county;
}
+ (void)setup {
[[FCXRating sharedRating] requestIP:NULL];
}
+ (void)startRating:(NSString *)appID finish:(void (^)(BOOL))finish {
return [[FCXRating sharedRating] fcx_startRating:appID finish:finish];
}
+ (FCXRating *)sharedRating {
static FCXRating *rating;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
rating = [[FCXRating alloc] init];
});
return rating;
}
- (void)fcx_startRating:(NSString *)appID finish:(void (^)(BOOL))finish {
BOOL showRating = [[FCXOnlineConfig fcxGetConfigParams:@"showRating" defaultValue:@"0"] boolValue];
if (!showRating) {
if (finish) {
finish(NO);
}
return;
}
[self checkAppVersion];
if (self.hasRating) {
if (finish) {
finish(NO);
}
return;
}
BOOL judgeIP = [FCXOnlineConfig fcxGetConfigParams:@"judgeIP" defaultValue:@"1"].boolValue;
if (judgeIP) {//判断IP
NSDictionary *timeDict = [FCXOnlineConfig fcxGetJSONConfigParams:@"ratingTime"];
NSInteger min = 10, max = 21;
if (timeDict) {
min = [timeDict[@"min"] integerValue];
max = [timeDict[@"max"] integerValue];
}
NSInteger hour = [self getCurrentDate:@"HH" forKey:@"RatingDateFormatter_Hour"].integerValue;
if (hour < min || hour > max) {//时间段不满足
if (finish) {
finish(NO);
}
return;
}
if (_county) {
if ([_county isEqualToString:@"中国"]) {
[self showRating:appID finish:finish];
} else {
if (finish) {
finish(NO);
}
}
} else {
[self requestIP:^(NSString *county) {
if (county && [county isEqualToString:@"中国"]) {
[self showRating:appID finish:finish];
} else {
if (finish) {
finish(NO);
}
}
}];
}
} else {//不判断IP
[self showRating:appID finish:finish];
}
}
- (void)showRating:(NSString *)appID finish:(void (^)(BOOL))finish {
NSDictionary *paramsDict = [FCXOnlineConfig fcxGetJSONConfigParams:@"ratingContent"];
if (![paramsDict isKindOfClass:[NSDictionary class]]) {
if (finish) {
finish(NO);
}
return;
}
// NSLog(@"==%@", paramsDict);
NSString *title = [paramsDict objectForKey:@"标题"];
NSString *content = [paramsDict objectForKey:@"内容"];
NSString *btn1 = [paramsDict objectForKey:@"按钮1"];
NSString *btn2 = [paramsDict objectForKey:@"按钮2"];
// NSString *btn3 = [paramsDict objectForKey:@"按钮3"];
NSInteger alertTimes = [[paramsDict objectForKey:@"总提醒次数"] integerValue];
if (!title || !content || !btn1 || !btn2) {
if (finish) {
finish(NO);
}
return;
}
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *currentDateString = [self getCurrentDate:@"YYYY-MM-dd" forKey:@"RatingDateFormatter"];
NSString *alertDateString = [userDefaults objectForKey:@"alertDate"];
if (alertDateString && [alertDateString isEqualToString:currentDateString]) {//当天弹出过
if (finish) {
finish(NO);
}
return;
}
if ([userDefaults integerForKey:@"alertTimes"] >= alertTimes) {//超过弹出次数
if (finish) {
finish(NO);
}
return;
}
MAlertViw *alertView = [[MAlertViw alloc] initWithTitle:title message:content delegate:nil cancelButtonTitle:nil otherButtonTitles:btn1, btn2, nil];
alertView.dismiss = YES;
[alertView show];
alertView.handleAction = ^(MAlertViw *alertView, NSInteger buttonIndex){
if (buttonIndex == 0) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
[vc presentViewController:[UMFeedback feedbackModalViewController] animated:YES completion:^{
}];
});
}else if(buttonIndex == 1) {
[FCXRating goRating:appID];
[FCXRating saveRating];
}else {
// [FCXRating saveRating];
}
};
[self saveAlert];
if (finish) {
finish(YES);
}
}
//保存提醒的日期和次数
- (void)saveAlert {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *currentDateString = [self getCurrentDate:@"YYYY-MM-dd" forKey:@"RatingDateFormatter"];
NSInteger alertTimes = [userDefaults integerForKey:@"alertTimes"];
alertTimes++;
[userDefaults setObject:currentDateString forKey:@"alertDate"];
[userDefaults setObject:[NSNumber numberWithInteger:alertTimes] forKey:@"alertTimes"];
[userDefaults synchronize];
}
//获取当前时间的字符串
- (NSString *)getCurrentDateString11 {
NSMutableDictionary *threadDictionary = [[NSThread currentThread] threadDictionary] ;
NSDateFormatter *dateFormatter = [threadDictionary objectForKey: @"RatingDateFormatter"] ;
if (dateFormatter == nil)
{
dateFormatter = [[NSDateFormatter alloc] init] ;
[dateFormatter setDateFormat: @"YYYY-MM-dd"] ;
[threadDictionary setObject: dateFormatter forKey: @"RatingDateFormatter"] ;
}
return [dateFormatter stringFromDate:[NSDate date]];
}
//获取当前时间的字符串
- (NSString *)getCurrentDate:(NSString *)dateFormatter forKey:(NSString *)key {
NSMutableDictionary *threadDictionary = [[NSThread currentThread] threadDictionary] ;
NSDateFormatter *_dateFormatter = [threadDictionary objectForKey:key] ;
if (_dateFormatter == nil)
{
_dateFormatter = [[NSDateFormatter alloc] init] ;
[_dateFormatter setDateFormat:dateFormatter];
[threadDictionary setObject:_dateFormatter forKey:key] ;
}
return [_dateFormatter stringFromDate:[NSDate date]];
}
//检查版本,如果版本不一致,清除之前版本的缓存
- (void)checkAppVersion {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *ratingVersion = [userDefaults objectForKey:@"RatingAppVersion"];
NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
if (!ratingVersion) {//之前没有存过版本,第一次存
ratingVersion = appVersion;
[userDefaults setObject:ratingVersion forKey:@"RatingAppVersion"];
}else if(![ratingVersion isEqualToString:appVersion]) {//版本升级,清空之前缓存
ratingVersion = appVersion;
[userDefaults setObject:ratingVersion forKey:@"RatingAppVersion"];
//清楚之前版本的缓存
[userDefaults removeObjectForKey:HASRATING];
[userDefaults removeObjectForKey:@"alertTimes"];
}
[userDefaults synchronize];
}
- (BOOL)hasRating {
return [[NSUserDefaults standardUserDefaults] boolForKey:HASRATING];
}
+ (void)saveRating {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:HASRATING];
[[NSUserDefaults standardUserDefaults]synchronize];
}
+ (void)goAppStore:(NSString*)appID {
if (![appID isKindOfClass:[NSString class]]) {
return;
}
// 打开应用内购买
SKStoreProductViewController *vc = [[SKStoreProductViewController alloc] init];
vc.delegate = [FCXRating sharedRating];
NSDictionary *dict = [NSDictionary dictionaryWithObject:appID forKey:SKStoreProductParameterITunesItemIdentifier];
[vc loadProductWithParameters:dict completionBlock:nil];
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:vc animated:YES completion:nil];
}
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {
[viewController dismissViewControllerAnimated:YES completion:nil];
}
+ (void)goRating:(NSString *)appID {
if (![appID isKindOfClass:[NSString class]]) {
return;
}
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat: @"itms-apps://itunes.apple.com/app/id%@?action=write-review", appID]];
UIApplication *application = [UIApplication sharedApplication];
if ([application canOpenURL:url]) {
[application openURL:url];
return;
}
url = [NSURL URLWithString:[NSString stringWithFormat: @"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%@", appID]];
if ([application canOpenURL:url]) {
[application openURL:url];
return;
}
url = [NSURL URLWithString:[NSString stringWithFormat: @"https://itunes.apple.com/app/id%@", appID]];
if ([application canOpenURL:url]) {
[application openURL:url];
return;
}
}
- (void)requestIP:(void (^)(NSString *county))finish {
NSString *urlString = @"http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
[request setTimeoutInterval: 2];
[request setHTTPShouldHandleCookies:FALSE];
[request setHTTPMethod:@"GET"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (data) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
// NSLog(@"json ==%@ ==%@", dict, dict[@"country"]);
if ([dict isKindOfClass:[NSDictionary class]]) {
_county = dict[@"country"];
if (finish) {
dispatch_async(dispatch_get_main_queue(), ^{
finish(_county);
});
}
} else if (finish) {
dispatch_async(dispatch_get_main_queue(), ^{
finish(nil);
});
}
} else if (finish) {
dispatch_async(dispatch_get_main_queue(), ^{
finish(nil);
});
}
/*
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSLog(@"respod %@ header %@", response,[(NSHTTPURLResponse *)response allHeaderFields]);
NSString *dateString = [[(NSHTTPURLResponse *)response allHeaderFields] objectForKey:@"Date"];
dateString = [dateString substringWithRange:NSMakeRange(5, 20)];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd MMM yyyy HH:mm:ss"];
dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
dateFormatter.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"];
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT+0800"]];
dateString = [dateFormatter stringFromDate:date];
NSInteger house = [[dateString substringWithRange:NSMakeRange(12, 2)] integerValue];
if (house >= 11 && house <= 20) {//符合时间
}
}
*/
}];
[task resume];
}
@end