iOS版本更新判断

//
//  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

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值