iOS自动检测版本并强制更新

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
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/lookup?id=AppID"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (data.length>0)
{
NSMutableDictionary* appDic = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSArray *resultArray = [appDic objectForKey:@"results"];
if ([resultArray count]>0)
{
NSString * appConnect = [[NSString alloc]
initWithContentsOfURL:[NSURL URLWithString:[resultArray.firstObject objectForKey:@"trackViewUrl"]]
encoding:NSUTF8StringEncoding
error:nil];
NSString* str=[appConnect componentsSeparatedByString:@"l-column small-6 medium-12 whats-new__latest__version\">版本"].lastObject;
str=[str componentsSeparatedByString:@"</p >"].firstObject;
NSArray* arr=[str componentsSeparatedByString:@"."];
NSInteger updateVersion=0;
for (int i=0; i<arr.count; i++)
{
if(i==0)
{
updateVersion+=[arr[i] integerValue]*1000;
}
else if (i==1)
{
updateVersion+=[arr[i] integerValue]*100;
}
else if (i==2)
{
updateVersion+=[arr[i] integerValue]*10;
}
else if (i==3)
{
updateVersion+=[arr[i] integerValue]*1;
}
}
NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
NSArray* arr2=[[infoDic objectForKey:@"CFBundleShortVersionString"] componentsSeparatedByString:@"."];
NSInteger currentVersion=0;
for (int i=0; i<arr2.count; i++)
{
if(i==0)
{
currentVersion+=[arr2[i] integerValue]*1000;
}
else if (i==1)
{
currentVersion+=[arr2[i] integerValue]*100;
}
else if (i==2)
{
currentVersion+=[arr2[i] integerValue]*10;
}
else if (i==3)
{
currentVersion+=[arr2[i] integerValue]*1;
}
}
dispatch_async(dispatch_get_main_queue(), ^{
if (currentVersion < updateVersion)
{
UIAlertController * alertC = [UIAlertController alertControllerWithTitle:@"版本更新" message:resultArray.firstObject[@"releaseNotes"] preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * doneAction = [UIAlertAction actionWithTitle:@"去升级" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSString *appURLstr = [resultArray.firstObject objectForKey:@"trackViewUrl"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:appURLstr]];
}];
[alertC addAction:doneAction];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:alertC animated:YES completion:nil];
});
}
});
}
}
}];
[dataTask resume];
});