//
// SHUpdate.m
// shyc
//
// Created by 李凌辉 on 2017/11/28.
// Copyright © 2017年 shyc. All rights reserved.
//
import “SHUpdate.h”
@implementation SHUpdate
// 获取自身版本号
+(NSString *)getLocalVersion{
NSString *localVersion = [[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleShortVersionString"];
return localVersion;
}
// 获取appstore版本号
+(void)getAppStoreVersion{
//定义的App地址
NSString *urlStr = [NSString stringWithFormat:@”http://itunes.apple.com/lookup?id=%@“,AppID];
//AppID即是如图红色箭头获取的AppID
//PS:有的时候可能会请求不到数据,但是AppID对了,有可能是App是上架区域范围的原因,建议使用在com末尾加上“/cn”
//例:NSString *url = [NSString stringWithFormat:@”http://itunes.apple.com/cn/lookup?id=%@“,AppID];
//网络请求App的信息(我们取Version就够了)
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:10];
[request setHTTPMethod:@”POST”];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask task = [session dataTaskWithRequest:request completionHandler:^(NSData _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSMutableDictionary *receiveStatusDic=[[NSMutableDictionary alloc]init];
if (data) {
//data是有关于App所有的信息
NSDictionary *receiveDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
if ([[receiveDic valueForKey:@”resultCount”] intValue]>0) {
[receiveStatusDic setValue:@"1" forKey:@"status"];
[receiveStatusDic setValue:[[[receiveDic valueForKey:@"results"] objectAtIndex:0] valueForKey:@"version"] forKey:@"version"];
//请求的有数据,进行版本比较
[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:@"."];
if ((versionArray.count == 3) && (localArray.count == versionArray.count)) {
if ([localArray[0] intValue] < [versionArray[0] intValue]) {
[self updateVersion];
}else if ([localArray[0] intValue] == [versionArray[0] intValue]){
if ([localArray[1] intValue] < [versionArray[1] intValue]) {
[self updateVersion];
}else if ([localArray[1] intValue] == [versionArray[1] intValue]){
if ([localArray[2] intValue] < [versionArray[2] intValue]) {
[self updateVersion];
}
}
}
}
}
// 升级提示及跳转
+(void)updateVersion{
NSString *msg = [NSString stringWithFormat:@"又出新版本啦,快点更新吧!"];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"升级提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
// Create the actions.
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"下次再说" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"现在升级"style:UIAlertActionStyleDestructive handler:^(UIAlertAction*action) {
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/us/app/id%@?mt=8",AppID]];
[[UIApplication sharedApplication]openURL:url];
}];
// Add the actions.
[alertController addAction:cancelAction];
[alertController addAction:otherAction];
[KEYWINDOW.rootViewController presentViewController:alertController animated:YES completion:nil];
}
@end