forked from biocross/VITacademics-v2-for-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pigeon.m
executable file
·175 lines (143 loc) · 6.78 KB
/
Pigeon.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
// The MIT License (MIT)
//
// Copyright (c) 2013 LIGHT [email protected]
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "Pigeon.h"
@interface Pigeon()
@property (assign, nonatomic) NSString *appleId;
@end
@implementation Pigeon
+ (instancetype)sharedInstance
{
static Pigeon *instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[[self class] alloc] init];
instance.updateMessage = [NSString stringWithFormat:[instance localizedStringForKey:@"UpdateMessage"], [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"]];
instance.notifyInterval = 3600 * 24;
});
return instance;
}
- (void)startWithAppleId:(NSString *)appleId
{
self.appleId = appleId;
if (![self shouldCheck]) return;
if (!self.latestVersion) {
dispatch_async(dispatch_queue_create("Pigeon", NULL), ^{
[self fetchLatestVersionFromAppStore];
if (![self isLatestVersion]) {
[[NSNotificationCenter defaultCenter] postNotificationName:PigeonDidFindNewVersionNotification object:nil];
}
});
} else {
if (![self isLatestVersion]) {
[[NSNotificationCenter defaultCenter] postNotificationName:PigeonDidFindNewVersionNotification object:nil];
}
}
}
- (void)openInAppStore
{
if (![NSThread isMainThread]) {
[self performSelectorOnMainThread:@selector(openInAppStore) withObject:nil waitUntilDone:YES];
return;
}
NSString *url = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/id%@", self.appleId];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
#pragma mark
- (BOOL)shouldCheck
{
NSTimeInterval currentTimeInterval = [[NSDate date] timeIntervalSince1970];
NSTimeInterval lastCheckedTimeInterval = [[NSUserDefaults standardUserDefaults] floatForKey:@"PIGEON_LAST_CHECKED_TIME_INTERVAL"];
if (currentTimeInterval <= lastCheckedTimeInterval + self.notifyInterval) return NO;
[[NSUserDefaults standardUserDefaults] setFloat:currentTimeInterval forKey:@"PIGEON_LAST_CHECKED_TIME_INTERVAL"];
[[NSUserDefaults standardUserDefaults] synchronize];
return YES;
}
- (BOOL)isLatestVersion
{
NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
NSArray *latestVersionArray = [self.latestVersion componentsSeparatedByString:@"."];
NSArray *currentVersionArray = [currentVersion componentsSeparatedByString:@"."];
for (NSInteger i = 0; i < latestVersionArray.count; i++) {
if (currentVersionArray.count <= i) return NO;
if ([latestVersionArray[i] intValue] > [currentVersionArray[i] intValue]) return NO;
if ([latestVersionArray[i] intValue] < [currentVersionArray[i] intValue]) return YES;
if ([latestVersionArray[i] intValue] == [currentVersionArray[i] intValue]) continue;
}
return YES;
}
- (void)fetchLatestVersionFromAppStore
{
NSError *error = nil;
NSString *countryCode = self.countyCode.length ? [self.countyCode stringByAppendingString:@"/"] : @"";
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/%@lookup?id=%@", countryCode, self.appleId]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
if (response && !error) {
NSDictionary *infoDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
if (!infoDic[@"results"]) return;
if ([infoDic[@"results"] count] == 0) return;
self.latestVersion = infoDic[@"results"][0][@"version"];
}
}
#pragma mark - Local Notification
- (void)enableLocalNotification
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addObserverOnApplicationDidEnterBackground) name:PigeonDidFindNewVersionNotification object:nil];
}
- (void)addObserverOnApplicationDidEnterBackground
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scheduleUpdateNotification) name:UIApplicationDidEnterBackgroundNotification object:nil];
}
- (void)scheduleUpdateNotification
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [[[NSDate alloc] init] dateByAddingTimeInterval:3];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = self.updateMessage;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
#pragma mark - Localization
- (NSString *)localizedStringForKey:(NSString *)key
{
return [[self localizedBundle] localizedStringForKey:key value:nil table:nil];
}
- (NSBundle *)localizedBundle
{
static NSBundle *localizedBundle = nil;
if (localizedBundle == nil) {
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"Pigeon" ofType:@"bundle"];
localizedBundle = [NSBundle bundleWithPath:bundlePath] ?: [NSBundle mainBundle];
for (NSString *language in [NSLocale preferredLanguages]) {
if ([[localizedBundle localizations] containsObject:language]) {
bundlePath = [localizedBundle pathForResource:language ofType:@"lproj"];
localizedBundle = [NSBundle bundleWithPath:bundlePath];
break;
}
}
if (localizedBundle == nil) {
bundlePath = [localizedBundle pathForResource:@"en" ofType:@"lproj"];
localizedBundle = [NSBundle bundleWithPath:bundlePath];
}
}
return localizedBundle;
}
@end