//
// AppUpdate.h
// iOSGeneral
//
// Created by XX on 2018/4/23.
// Copyright © 2018年 XX. All rights reserved.
//
#import <Foundation/Foundation.h>
#define myappid @""//这里写appid
typedef enum : NSUInteger {
UpdateOptional,//可选
UpdateForbidCancel,//不允许取消,强制更新
UpdateNow,//立即去App Store更新
} UpdateFlag;
@interface AppUpdate : NSObject
- (void)versionUpdate:(UpdateFlag)flag;
@end
//
// AppUpdate.m
// iOSGeneral
//
// Created by XX on 2018/4/23.
// Copyright © 2018年 XX. All rights reserved.
//
#import "AppUpdate.h"
#import "AppUpdateViewController.h"
#import "LWAUodateViewController.h"
@interface AppUpdate()
@end
@implementation AppUpdate
- (void)versionUpdate:(UpdateFlag)flag {
//定义的app的地址
//country 上架国家(全部国家可不写) id appid。
NSString *urld = [NSString stringWithFormat:@"https://itunes.apple.com/lookup?country=cn&id=%@", myappid];
NSURL *url = [NSURL URLWithString:urld];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
[request setHTTPMethod:@"GET"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSMutableDictionary *receiveStatusDic = [[NSMutableDictionary alloc] init];
if (data) {
NSDictionary *receiveDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves|NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:nil];
NSArray *array =receiveDic[@"results"];
//获取appstore最新版本号
NSString *version = [array[0] objectForKey:@"version"];
NSString *note = [array[0] objectForKey:@"releaseNotes"];
if ([version intValue]>0) {
[receiveStatusDic setValue:@"1" forKey:@"status"];
[receiveStatusDic setValue:version forKey:@"version"];
[receiveStatusDic setValue:note forKey:@"note"];
[receiveStatusDic setValue:@(flag) forKey:@"updateFlag"];
//请求的有数据,进行版本比较
[self performSelectorOnMainThread:@selector(receiveData:) withObject:receiveStatusDic waitUntilDone:NO];
} else {
[receiveStatusDic setValue:@"-1" forKey:@"status"];
}
} else {
[receiveStatusDic setValue:@"-1" forKey:@"status"];
}
}];
[task resume];
}
- (void)receiveData:(id)sender {
//获取APP自身版本号
NSString *localVersion = [[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleShortVersionString"];
NSArray *localArray = [localVersion componentsSeparatedByString:@"."];
NSArray *versionArray = [sender[@"version"] componentsSeparatedByString:@"."];
//比较
NSUInteger count = versionArray.count;
if (versionArray.count > localArray.count) {
count = localArray.count;
}
for (int i = 0; i < count; i++) {
if ([localArray[i] intValue] < [versionArray[i] intValue]) {
[self alertUpdateVersion:sender];
return;
}
//审核时本地大于线上,开发时也有可能
if ([localArray[i] intValue] > [versionArray[i] intValue]) {
NSLog(@"本地版本大于线上");
return;
}
}
//出现了1.2和1.2.3的情况,肯定是长的那个是高版本,因为不存在1.2和1.2.0对比的情况
if (versionArray.count < localArray.count) {
[self alertUpdateVersion:sender];
return;
}
NSLog(@"目前没有新版本");
if ([sender[@"updateFlag"] integerValue] == UpdateNow) {
[DWBToast showCenterWithText:@"当前版本已是最新版本,无需更新"];
}
}
- (void)alertUpdateVersion:(NSDictionary *)dic{
[defaults setObject:@"update" forKey:@"update"];
if ([dic[@"updateFlag"] integerValue] == UpdateForbidCancel) {
[LWAUodateViewController AlertWithVersion:dic[@"version"] content:dic[@"note"] leftAction:^{
[self goToUpdate];
}];
}else if ([dic[@"updateFlag"] integerValue] == UpdateOptional){
[AppUpdateViewController AlertWithVersion:dic[@"version"] content:dic[@"note"] leftAction:^{
[self goToUpdate];
} rightAction:^{
//取消事件
}];
} else if ([dic[@"updateFlag"] integerValue] == UpdateNow) {
[self goToUpdate];
}
// NSString *msg = [NSString stringWithFormat:@"更新最新版本"];
// UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"升级提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
// UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"现在升级"style:UIAlertActionStyleDestructive handler:^(UIAlertAction*action) {
// //跳转地址链接
// NSString *str = [NSString stringWithFormat: @"itms-apps://itunes.apple.com/cn/app/id%@?mt=8", myappid];
// UIApplication *app = [UIApplication sharedApplication];
// NSURL *url = [NSURL URLWithString:str];
// if ([app canOpenURL:url])
// {
// [app openURL:url];
//
// }
//
// }];
// [alertController addAction:otherAction];
//
// [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
}
- (void)goToUpdate {
//跳转地址链接
NSString *str = [NSString stringWithFormat: @"itms-apps://itunes.apple.com/cn/app/id%@?mt=8", myappid];
UIApplication *app = [UIApplication sharedApplication];
NSURL *url = [NSURL URLWithString:str];
if ([app canOpenURL:url]) {
[app openURL:url];
}
}
@end