-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSJCVersion.m
256 lines (222 loc) · 11.4 KB
/
SJCVersion.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
//
// SJCVersion.m
// 版本更新
//
// Created by 时光与你 on 2019/8/20.
// Copyright © 2019 Yehwang. All rights reserved.
//
#import "SJCVersion.h"
NSString * const SJCVersionDefaultSkippedVersion = @"SJCVersion User Decided To Skip Version Update Boolean";
NSString * const SJCVersionDefaultStoredVersionCheckDate = @"SJCVersion Stored Date From Last Version Check";
NSString * const SJCVersionDidShowNotification = @"SJCVersionDidShowNotification";
NSString * const SJCVersionDidLaunchAppStoreNotification = @"SJCVersionDidLaunchAppStoreNotification";
NSString * const SJCVersionDidSkipVersionNotification = @"SJCVersionDidSkipVersionNotification";
NSString * const SJCVersionDidCancelNotification = @"SJCVersionDidCancelNotification";
NSString * const SJCVersionDidDetectNewVersionWithoutAlertNotification = @"SJCVersionDidDetectNewVersionWithoutAlertNotification";
@interface SJCVersion ()
@property (nonatomic, strong) NSDictionary <NSString *, id> *appData;
@property (nonatomic, strong) NSDate *lastVersionCheckPerformedOnDate;
@property (nonatomic, copy) NSString *appID;
@property (nonatomic, copy) NSString *currentInstalledVersion;
@property (nonatomic, copy) NSString *currentAppStoreVersion;
@end
@implementation SJCVersion
+ (SJCVersion *)sharedInstance {
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
- (id)init {
if (self = [super init]) {
_alertType = SJCVersionAlertTypeSkip;
_lastVersionCheckPerformedOnDate = [[NSUserDefaults standardUserDefaults] objectForKey:SJCVersionDefaultStoredVersionCheckDate];
_currentInstalledVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
}
return self;
}
- (void)checkVersion {
[self performSelectorInBackground:@selector(startCheck) withObject:nil];
}
- (void)checkVersionDaily {
if (![self lastVersionCheckPerformedOnDate]) {
self.lastVersionCheckPerformedOnDate = [NSDate date];
[self checkVersion];
}
if ([self numberOfDaysElapsedBetweenLastVersionCheckDate] >= 1) {
[self checkVersion];
}
}
- (void)checkVersionWeekly {
if (![self lastVersionCheckPerformedOnDate]) {
self.lastVersionCheckPerformedOnDate = [NSDate date];
[self checkVersion];
}
if ([self numberOfDaysElapsedBetweenLastVersionCheckDate] >= 7) {
[self checkVersion];
}
}
- (void)startCheck {
NSURL *storeURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/lookup?bundleId=%@",[NSBundle mainBundle].bundleIdentifier]];
NSURLRequest *request = [NSMutableURLRequest requestWithURL:storeURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0];
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if ([data length] > 0 && !error) {
[self handelResults:data];
}
}];
[task resume];
}
- (void)handelResults:(NSData *)data {
_appData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
__typeof__(self) __weak weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
weakSelf.lastVersionCheckPerformedOnDate = [NSDate date];
[[NSUserDefaults standardUserDefaults] setObject:self.lastVersionCheckPerformedOnDate forKey:SJCVersionDefaultStoredVersionCheckDate];
[[NSUserDefaults standardUserDefaults] synchronize];
NSDictionary<NSString *, id> *results = [self.appData valueForKey:@"results"];
NSArray *versionsInAppStore = [results valueForKey:@"version"];
if (versionsInAppStore != nil) {
if ([versionsInAppStore count]) {
weakSelf.currentAppStoreVersion = [versionsInAppStore objectAtIndex:0];
if ([weakSelf.currentInstalledVersion compare:weakSelf.currentAppStoreVersion options:NSNumericSearch] == NSOrderedAscending) {
[weakSelf appStoreVersionIsNewer:weakSelf.currentAppStoreVersion];
}
}
}
});
}
- (void)appStoreVersionIsNewer:(NSString *)currentAppStoreVersion {
_appID = _appData[@"results"][0][@"trackId"];
if (_appID != nil) {
[self showAlertIfCurrentAppStoreVersionNotSkipped:currentAppStoreVersion];
}
}
- (void)showAlertIfCurrentAppStoreVersionNotSkipped:(NSString *)currentAppStoreVersion {
NSString *storedSkippedVersion = [[NSUserDefaults standardUserDefaults] objectForKey:SJCVersionDefaultSkippedVersion];
if (![storedSkippedVersion isEqualToString:currentAppStoreVersion]) {
[self showAlertWithAppStoreVersion:currentAppStoreVersion];
}
}
- (void)showAlertWithAppStoreVersion:(NSString *)currentAppStoreVersion {
switch (self.alertType) {
case SJCVersionAlertTypeForce: {
UIAlertController *alertController = [self createAlertController];
[alertController addAction:[self updateAlertAction]];
[self showAlertController:alertController];
}break;
case SJCVersionAlertTypeOption: {
UIAlertController *alertController = [self createAlertController];
[alertController addAction:[self nextTimeAlertAction]];
[alertController addAction:[self updateAlertAction]];
[self showAlertController:alertController];
}break;
case SJCVersionAlertTypeSkip: {
UIAlertController *alertController = [self createAlertController];
[alertController addAction:[self updateAlertAction]];
[alertController addAction:[self nextTimeAlertAction]];
[alertController addAction:[self skipAlertAction]];
[self showAlertController:alertController];
}break;
case SJCVersionAlertTypeNone: {
[[NSNotificationCenter defaultCenter] postNotificationName:SJCVersionDidDetectNewVersionWithoutAlertNotification
object:self
userInfo:nil];
}break;
}
}
- (void)showAlertController:(UIAlertController *)alertController {
if ([self presentingViewController] != nil) {
[[self presentingViewController] presentViewController:alertController animated:YES completion:nil];
}
[[NSNotificationCenter defaultCenter] postNotificationName:SJCVersionDidShowNotification
object:self
userInfo:nil];
}
- (NSUInteger)numberOfDaysElapsedBetweenLastVersionCheckDate {
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDateComponents *components = [currentCalendar components:NSCalendarUnitDay
fromDate:self.lastVersionCheckPerformedOnDate
toDate:[NSDate date]
options:0];
return [components day];
}
- (UIViewController *)presentingViewController{
UIViewController *root = [[UIApplication sharedApplication] delegate].window.rootViewController;
if ([root isKindOfClass:[UINavigationController class]]){
UINavigationController *navRoot = (UINavigationController *)root;
return navRoot.topViewController;
}
else if ([root isKindOfClass:[UITabBarController class]]){
UITabBarController *tabRoot = (UITabBarController *)root;
UIViewController *tabSelect = tabRoot.selectedViewController;
if ([tabSelect isKindOfClass:[UINavigationController class]]){
UINavigationController *tabSelectNav = (UINavigationController *)tabSelect;
return tabSelectNav.topViewController;
}
else{
return tabSelect;
}
}else{
return root;
}
return nil;
}
- (UIAlertController *)createAlertController {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"有新版本可用"
message:[NSString stringWithFormat:@"你的应用%@有新的版本%@,快去App Store更新吧!",[[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleNameKey],self.currentAppStoreVersion]
preferredStyle:UIAlertControllerStyleAlert];
return alertController;
}
- (UIAlertAction *)updateAlertAction {
UIAlertAction *updateAlertAction = [UIAlertAction actionWithTitle:@"更新"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[self launchAppStore];
}];
return updateAlertAction;
}
- (UIAlertAction *)nextTimeAlertAction {
UIAlertAction *nextTimeAlertAction = [UIAlertAction actionWithTitle:@"下一次"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[self nextTime];
}];
return nextTimeAlertAction;
}
- (UIAlertAction *)skipAlertAction {
UIAlertAction *skipAlertAction = [UIAlertAction actionWithTitle:@"忽略此版本"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) {
[self skip];
}];
return skipAlertAction;
}
- (void)launchAppStore {
NSString *iTunesString = [NSString stringWithFormat:@"https://itunes.apple.com/app/id%@", self.appID];
NSURL *iTunesURL = [NSURL URLWithString:iTunesString];
dispatch_async(dispatch_get_main_queue(), ^{
if (@available(iOS 10.0, *)) {
[[UIApplication sharedApplication] openURL:iTunesURL options:@{} completionHandler:nil];
} else {
[[UIApplication sharedApplication] openURL:iTunesURL];
}
[[NSNotificationCenter defaultCenter] postNotificationName:SJCVersionDidLaunchAppStoreNotification
object:self
userInfo:nil];
});
}
- (void)nextTime {
[[NSNotificationCenter defaultCenter] postNotificationName:SJCVersionDidCancelNotification
object:self
userInfo:nil];
}
- (void)skip{
[[NSUserDefaults standardUserDefaults] setObject:self.currentAppStoreVersion forKey:SJCVersionDefaultSkippedVersion];
[[NSUserDefaults standardUserDefaults] synchronize];
[[NSNotificationCenter defaultCenter] postNotificationName:SJCVersionDidSkipVersionNotification
object:self
userInfo:nil];
}
@end