您的位置:首页 > 移动开发 > IOS开发

利用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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: