利用pre平台实现iOS应用程序自动更新
2015-12-18 19:16
447 查看
// // AppDelegate.m // PreAutoUpdateDemo // // Created by mac on 15/12/18. // Copyright © 2015年 mac. All rights reserved. // #import "AppDelegate.h" #define USER_KEY @"1234321344SDFDFBVVFGDSVF" // 根据实际情况替换为自己的user_key @interface AppDelegate () <UIAlertViewDelegate> /** pre app_key */ @property(nonatomic,copy)NSString *app_key; /** package_key */ @property(nonatomic,copy)NSString *package_key; /** last_version */ @property(nonatomic,copy)NSString *last_version; /** isNewVirson 是否有新版本需要更新 */ @property(nonatomic,assign)BOOL isNewVersion; @end @implementation AppDelegate /* 实现程序启动就能动态检测是否有更新需要在application:(UIApplication *)application didFinishLaunchingWithOptions:方法里 1.首先调用viewUploadApps方法获取app_key 2.然后调用getAllVersions方法获取package_key 3.再调用getAppDetailInfo获取last_version 4.最后调用compareVersions方法比较版本号 5.如果版本号不同那么就调用updateApp方法更新喽 备注:不懂加QQ:1838886973 */ # pragma mark - pre自动更新 // 获取app_key - (void)viewUploadApps { // POST-http://pre.im/api/v1/app/myapps NSURLSession *session = [NSURLSession sharedSession]; NSURL *url = [NSURL URLWithString:@"http://pre.im/api/v1/app/myapps"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = @"POST"; request.HTTPBody = [@"user_key=cedf0edc71e463628af1ee9c4b3bb84b" dataUsingEncoding:NSUTF8StringEncoding]; NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { if (!data) { return; } NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]; self.app_key = [dict[@"data"][@"list"] firstObject][@"app_key"]; dispatch_async(dispatch_get_main_queue(), ^{ [self getAllVersions]; }); }]; [task resume]; } // 获取package_key - (void)getAllVersions { // POST-http://pre.im/api/v1/app/builds NSURLSession *session = [NSURLSession sharedSession]; NSURL *url = [NSURL URLWithString:@"http://pre.im/api/v1/app/builds"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = @"POST"; NSString *parms = [NSString stringWithFormat:@"user_key=%@&app_key=%@&page=%d",USER_KEY,self.app_key,1]; request.HTTPBody = [parms dataUsingEncoding:NSUTF8StringEncoding]; NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { if (!data) { return; } NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]; self.package_key = [dict[@"data"][@"list"] firstObject][@"package_key"]; dispatch_async(dispatch_get_main_queue(), ^{ [self getAppDetailInfo]; }); }]; [task resume]; } // 获取last_version - (void)getAppDetailInfo { // POST-http://pre.im/api/v1/app/view NSURLSession *session = [NSURLSession sharedSession]; NSURL *url = [NSURL URLWithString:@"http://pre.im/api/v1/app/view"]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; request.HTTPMethod = @"POST"; NSString *parms = [NSString stringWithFormat:@"user_key=%@&app_key=%@&package_key=%@",USER_KEY,self.app_key,self.package_key]; request.HTTPBody = [parms dataUsingEncoding:NSUTF8StringEncoding]; NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { if (!data) { return; } NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]; self.last_version = dict[@"data"][@"last_version"]; dispatch_async(dispatch_get_main_queue(), ^{ [self compareVersions]; }); }]; [task resume]; } // 比较版本号 - (void)compareVersions { NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; // app当前版本 NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"]; if (app_Version.floatValue < self.last_version.floatValue) { // 注意了,这里只能比较2.1、2.2这种版本号,不能比较2.1.1这种三段式版本。如果想比较三段式版本,可以把if的判断条件改为比较字符串是否相同 self.isNewVersion = YES; // 更新 dispatch_async(dispatch_get_main_queue(), ^{ UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"App更新" message:@"有最新版本更新哦~" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"更新", nil]; [alertView show]; }); } else { self.isNewVersion = NO; } } //更新APP - (void)updateApp { // GET-http://pre.im/api/v1/app/install NSURLSession *session = [NSURLSession sharedSession]; NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://pre.im/api/v1/app/install?app_key=%@",self.app_key]]; NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { if (!data) { return; } NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]; NSString *urlStr = dict[@"data"]; NSURL *url = [NSURL URLWithString:urlStr]; BOOL isOpen = [[UIApplication sharedApplication] openURL:url]; NSLog(@"isOpen = %d url = %@",isOpen,dict[@"data"]); }]; [task resume]; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //pre更新 [self viewUploadApps]; return YES; } #pragma mark - UIAlertViewDelegate - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 1 && self.isNewVersion) { // 点击了确定就更新 [self updateApp]; } } @end
相关文章推荐
- IOS NSString NSMutableString 不可变|可变字符串
- NSDate iOS 日期
- IOS设备设计完整指南
- iOS Xcode工程目录的 folder 和 group的区别(蓝色和黄色文件夹的区别)
- iOS拼接字符串
- iOS邮箱正则表达式
- iOS判断为空
- IOS高仿三九脑科医院综合查询系统-列表查询+切换+代理传值+打开视图
- Xcode删除所有的描述文件
- iOS沙盒路径会变化
- IOS传值的几种方式
- 全面理解iOS开发中的Scroll View
- iOS APNS远程推送
- iOS开发 使用Xcode自带的Leaks:动态的检测内存泄露
- [IOS] NSOutputStream向输出流写数据
- [IOS] NSInputStream从输入流里读入数据
- iOS学习笔记1 -- 触摸与手势
- IOS判断日期是今天,昨天还是明天
- iOS ble关键开发步骤
- iOS之个人工作常用知识