1 //
2 // AppDelegate.m
3 // PreAutoUpdateDemo
4 //
5 // Created by mac on 15/12/18.
6 // Copyright © 2015年 mac. All rights reserved.
7 //
8
9 #import "AppDelegate.h"
10
11 #define USER_KEY @"1234321344SDFDFBVVFGDSVF" // 根据实际情况替换为自己的user_key
12
13 @interface AppDelegate () <UIAlertViewDelegate>
14
15 /** pre app_key */
16 @property(nonatomic,copy)NSString *app_key;
17 /** package_key */
18 @property(nonatomic,copy)NSString *package_key;
19 /** last_version */
20 @property(nonatomic,copy)NSString *last_version;
21 /** isNewVirson 是否有新版本需要更新 */
22 @property(nonatomic,assign)BOOL isNewVersion;
23 @end
24
25 @implementation AppDelegate
26 /*
27 实现程序启动就能动态检测是否有更新需要在application:(UIApplication *)application didFinishLaunchingWithOptions:方法里
28 1.首先调用viewUploadApps方法获取app_key
29 2.然后调用getAllVersions方法获取package_key
30 3.再调用getAppDetailInfo获取last_version
31 4.最后调用compareVersions方法比较版本号
32 5.如果版本号不同那么就调用updateApp方法更新喽
33
34 备注:不懂加QQ:1838886973
35 */
36
37 # pragma mark - pre自动更新
38 // 获取app_key
39 - (void)viewUploadApps
40 {
41 // POST-http://pre.im/api/v1/app/myapps
42 NSURLSession *session = [NSURLSession sharedSession];
43 NSURL *url = [NSURL URLWithString:@"http://pre.im/api/v1/app/myapps"];
44 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
45 request.HTTPMethod = @"POST";
46 request.HTTPBody = [@"user_key=cedf0edc71e463628af1ee9c4b3bb84b" dataUsingEncoding:NSUTF8StringEncoding];
47
48 NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
49 if (!data) {
50 return;
51 }
52 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
53 self.app_key = [dict[@"data"][@"list"] firstObject][@"app_key"];
54
55 dispatch_async(dispatch_get_main_queue(), ^{
56
57 [self getAllVersions];
58
59 });
60 }];
61
62 [task resume];
63 }
64 // 获取package_key
65 - (void)getAllVersions
66 {
67 // POST-http://pre.im/api/v1/app/builds
68 NSURLSession *session = [NSURLSession sharedSession];
69 NSURL *url = [NSURL URLWithString:@"http://pre.im/api/v1/app/builds"];
70 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
71 request.HTTPMethod = @"POST";
72 NSString *parms = [NSString stringWithFormat:@"user_key=%@&app_key=%@&page=%d",USER_KEY,self.app_key,1];
73 request.HTTPBody = [parms dataUsingEncoding:NSUTF8StringEncoding];
74
75 NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
76 if (!data) {
77 return;
78 }
79
80 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
81 self.package_key = [dict[@"data"][@"list"] firstObject][@"package_key"];
82
83 dispatch_async(dispatch_get_main_queue(), ^{
84
85 [self getAppDetailInfo];
86
87 });
88
89 }];
90
91 [task resume];
92 }
93 // 获取last_version
94 - (void)getAppDetailInfo
95 {
96 // POST-http://pre.im/api/v1/app/view
97 NSURLSession *session = [NSURLSession sharedSession];
98 NSURL *url = [NSURL URLWithString:@"http://pre.im/api/v1/app/view"];
99
100
101 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
102 request.HTTPMethod = @"POST";
103
104 NSString *parms = [NSString stringWithFormat:@"user_key=%@&app_key=%@&package_key=%@",USER_KEY,self.app_key,self.package_key];
105
106 request.HTTPBody = [parms dataUsingEncoding:NSUTF8StringEncoding];
107
108 NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
109 if (!data) {
110 return;
111 }
112
113 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
114 self.last_version = dict[@"data"][@"last_version"];
115
116 dispatch_async(dispatch_get_main_queue(), ^{
117
118 [self compareVersions];
119
120 });
121 }];
122 [task resume];
123
124 }
125 // 比较版本号
126 - (void)compareVersions
127 {
128 NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
129 // app当前版本
130 NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
131
132 if (app_Version.floatValue < self.last_version.floatValue) { // 注意了,这里只能比较2.1、2.2这种版本号,不能比较2.1.1这种三段式版本。如果想比较三段式版本,可以把if的判断条件改为比较字符串是否相同
133 self.isNewVersion = YES;
134 // 更新
135 dispatch_async(dispatch_get_main_queue(), ^{
136 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"App更新" message:@"有最新版本更新哦~" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"更新", nil];
137 [alertView show];
138 });
139 } else {
140 self.isNewVersion = NO;
141 }
142 }
143 //更新APP
144 - (void)updateApp
145 {
146 // GET-http://pre.im/api/v1/app/install
147 NSURLSession *session = [NSURLSession sharedSession];
148 NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://pre.im/api/v1/app/install?app_key=%@",self.app_key]];
149
150 NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
151 if (!data) {
152 return;
153 }
154 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
155 NSString *urlStr = dict[@"data"];
156 NSURL *url = [NSURL URLWithString:urlStr];
157
158 BOOL isOpen = [[UIApplication sharedApplication] openURL:url];
159 NSLog(@"isOpen = %d url = %@",isOpen,dict[@"data"]);
160
161 }];
162 [task resume];
163 }
164
165
166 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
167
168 //pre更新
169 [self viewUploadApps];
170 return YES;
171 }
172
173 #pragma mark - UIAlertViewDelegate
174 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
175 {
176 if (buttonIndex == 1 && self.isNewVersion) { // 点击了确定就更新
177 [self updateApp];
178 }
179 }
180 @end