-
Notifications
You must be signed in to change notification settings - Fork 1
/
FCXGuide.m
140 lines (109 loc) · 4.64 KB
/
FCXGuide.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
//
// FCXGuide.h
// Universial
//
// Created by 冯 传祥 on 15/8/23.
// Copyright (c) 2015年 冯 传祥. All rights reserved.
//
#import "FCXGuide.h"
#import "FCXOnlineConfig.h"
#import "UMMobClick/MobClick.h"
@implementation MAlertViw
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated {
if (self.dismiss) {
[super dismissWithClickedButtonIndex:buttonIndex animated:animated];
}
if (self.handleAction) {
self.handleAction(self, buttonIndex);
}
}
@end
@implementation FCXGuide
+ (void)startGuide {
if (![[FCXOnlineConfig fcxGetConfigParams:@"showGuide"] isEqualToString:@"1"]) {
return;
}
FCXGuide *guide = [[FCXGuide alloc] init];
[guide fcx_startGuide];
}
- (void)fcx_startGuide {
// NSString *paramsString = @"{ \"导流形式\" : \"1\", \"标题\" : \"铃声\", \"内容\" : \"导流测试\", \"左按钮\" : \"以后再说\", \"右按钮\" : \"马上下载\", \"appid\" : \"2\"}";
NSString *paramsString = [FCXOnlineConfig fcxGetConfigParams:@"guideContent" defaultValue:@""];
NSDictionary *paramsDict = [NSJSONSerialization JSONObjectWithData:[paramsString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
// NSLog(@"==%@", paramsDict);
NSString *type = [paramsDict objectForKey:@"导流形式"];
NSString *title = [paramsDict objectForKey:@"标题"];
NSString *content = [paramsDict objectForKey:@"内容"];
NSString *left = [paramsDict objectForKey:@"左按钮"];
NSString *right = [paramsDict objectForKey:@"右按钮"];
NSString *appid = [paramsDict objectForKey:@"appid"];
__block NSString *url = [paramsDict objectForKey:@"url"];
if (!type || !title || !content || !left) {
return;
}
if ([type isEqualToString:@"1"]) {
MAlertViw *alertView = [[MAlertViw alloc] initWithTitle:title message:content delegate:nil cancelButtonTitle:nil otherButtonTitles:left, nil];
[alertView show];
alertView.handleAction = ^(MAlertViw *alertView, NSInteger buttonIndex){
if (url && [url hasPrefix:@"http"]) {
[MobClick event:@"引导" label:url];
} else {
[MobClick event:@"引导" label:appid];
url = [NSString stringWithFormat: @"https://itunes.apple.com/us/app/id%@", appid];
}
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
};
}else {
if (!right) {
return;
}
//1每次 2每天
NSString *rate = [paramsDict objectForKey:@"弹出机制"];
if (![self shouldShowGuide:rate.integerValue]) {
return;
}
MAlertViw *alertView = [[MAlertViw alloc] initWithTitle:title message:content delegate:nil cancelButtonTitle:left otherButtonTitles:right, nil];
alertView.dismiss = YES;
[alertView show];
alertView.handleAction = ^(MAlertViw *alertView, NSInteger buttonIndex){
if (buttonIndex == 1) {
if (url && [url hasPrefix:@"http"]) {
[MobClick event:@"引导" label:url];
} else {
[MobClick event:@"引导" label:appid];
url = [NSString stringWithFormat: @"https://itunes.apple.com/us/app/id%@", appid];
}
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}else {
[MobClick event:@"引导" label:left];
}
};
}
}
///获取当前时间的字符串
- (NSString *)getCurrentDateString {
NSMutableDictionary *threadDictionary = [[NSThread currentThread] threadDictionary] ;
NSDateFormatter *dateFormatter = [threadDictionary objectForKey: @"GuideDateFormatter"] ;
if (dateFormatter == nil)
{
dateFormatter = [[NSDateFormatter alloc] init] ;
[dateFormatter setDateFormat: @"YYYY-MM-dd"] ;
[threadDictionary setObject: dateFormatter forKey: @"GuideDateFormatter"] ;
}
return [dateFormatter stringFromDate:[NSDate date]];
}
- (BOOL)shouldShowGuide:(NSInteger)rate {
if (rate == 1) {
return YES;
}
NSString *currentDateString = [self getCurrentDateString];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *guideDateString = [userDefaults objectForKey:@"guideDate"];
if (guideDateString && [guideDateString isEqualToString:currentDateString]) {
return NO;
}
[userDefaults setObject:currentDateString forKey:@"guideDate"];
[userDefaults synchronize];
return YES;
}
@end