-
Notifications
You must be signed in to change notification settings - Fork 34
/
Update.m
97 lines (78 loc) · 2.33 KB
/
Update.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
//
// file: Update.m
// project: KnockKnock
// description: checks for new versions of KnockKnock
//
// created by Patrick Wardle
// copyright (c) 2017 Objective-See. All rights reserved.
//
#import "Consts.h"
#import "Update.h"
#import "Utilities.h"
#import "AppDelegate.h"
@implementation Update
//check for an update
// ->will invoke app delegate method to update UI when check completes
-(void)checkForUpdate:(void (^)(NSUInteger result, NSString* latestVersion))completionHandler
{
//latest version
__block NSString* latestVersion = nil;
//result
__block NSInteger result = -1;
//get latest version in background
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//grab latest version
latestVersion = [self getLatestVersion];
if(nil != latestVersion)
{
//check
result = (NSOrderedAscending == [getAppVersion() compare:latestVersion options:NSNumericSearch]);
}
//invoke app delegate method
// ->will update UI/show popup if necessart
dispatch_async(dispatch_get_main_queue(),
^{
completionHandler(result, latestVersion);
});
});
return;
}
//query interwebz to get latest version
-(NSString*)getLatestVersion
{
//product version(s) data
NSData* productsVersionData = nil;
//version dictionary
NSDictionary* productsVersionDictionary = nil;
//latest version
NSString* latestVersion = nil;
//get version from remote URL
productsVersionData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:PRODUCT_VERSIONS_URL]];
if(nil == productsVersionData)
{
//bail
goto bail;
}
//convert JSON to dictionary
// ->wrap as may throw exception
@try
{
//convert
productsVersionDictionary = [NSJSONSerialization JSONObjectWithData:productsVersionData options:0 error:nil];
if(nil == productsVersionDictionary)
{
//bail
goto bail;
}
}
@catch(NSException* exception)
{
//bail
goto bail;
}
//extract latest version
latestVersion = [[productsVersionDictionary objectForKey:PRODUCT_NAME] objectForKey:@"version"];
bail:
return latestVersion;
}
@end