iOS开发技术iOS在路上知识总结

iOS 版本更新(强制更新)检测问题

2016-08-19  本文已影响13905人  清河湾

12月1日更新

谢谢,@搬砖技术的提醒
需要添加些注意事项:

版本号对比是需要相同的位数的情况下的

比如:位数不同的没法比较, 比如 2.3.10与2.4.1 和 2.3.9 与 2.3.10 , 这个时候 2.3.10 应该是变成2.4.0
如果是后续需要 多位的那应该也是 2.004.001 ,补足位数,才有可比性.

5月 18日 更新

这部分是之前对比有BUG的,现在已经修复了.


Paste_Image.png

------------------------------------------------------------------------------------------

原文

通常iOS系统中是默认设置再wifi状态,且网络状况良好下自己更新应用的.
但是如果用户设置了不自动更新,但是我们的APP出现重要的版本,一定需要用户更新的情况下,就会需要这个功能了.

这个版本更新一般会有两种方式:
1.在自己的服务器上部署上一个文件,写入版本数据,然后app去获取版本数据,与自己的当前版本比对, 提示更新
优点:可自定义更新模式(强制更新,选择更新)
缺点:APP审核的时间不可控
2.去AppStore获取当前发布的版本号,与用户当前版本比对,然后提示更新.
优点:版本更新的时间精准
缺点:自定义空间小
这两种方法一般推荐第2种....
需要自定义更新模式的(强制更新,选择更新)推荐使用第一种

//第二种
/*版本更新检测
284882215 (Facebook APP ID)

//中国区,需要再.com后面加 /cn 下面已经加,测试对于外国的APP也能行

#define APP_URL @"http://itunes.apple.com/cn/lookup?id=你程序的appId"

请求网络数据,返回的大致数据如下,其他还有好多数据,我们把关键的给截取出来

{  
    resultCount = 1;  
    results =     (  
                {  
            artistId = 开发者 ID;  
            artistName = 开发者名称; 
            price = 0; 
            isGameCenterEnabled = 0;  
            kind = software;  
            languageCodesISO2A =             (  
                EN  
            ); 
            trackCensoredName = 审查名称;  
            trackContentRating = 评级;  
            trackId = 应用程序 ID;  
            trackName = 应用程序名称";  
            trackViewUrl = 应用程序介绍网址(下载地址);  
            userRatingCount = 用户评级;  
            userRatingCountForCurrentVersion = 1;  
            version = 版本号;  
            wrapperType = software; 
      }  
    );  
}  
NSString *version =  responseObject[@"results"][@"version"]; //版本号
NSString *trackViewUrl =  responseObject[@"results"][@"trackViewUrl"]; //程序下载地址
取得这些数据后关键的信息就是“ version”最新版本号和“ trackViewUrl”程序下载地址。然后与本地程序的版本比较即可。
*/
##这个你可以参照下面第一种的过程都一样

请求数据 -> 判断版本 -> 提示或不提示-> 后续操作

//第一种

你首先要去后台配置这个版本更新文件,json文件,或者txt文件也行,可配置的,(后台搞定)

在进入程序的时候 去后台接口,请求这个数据
/*
示例 josn格式
AFNetworking请求回来responseObject的格式  (请求我就不写了,这种代码烂大街咯)
{
    iosDownload = "itms-apps://itunes.apple.com/cn/app/wang-yi-yun-yin-le-pao-bufm/id590338362?mt=8";
    iosForce = 0; //控制强制更新
    iosUpdateMsg = "更新信息,哈哈哈😄";
    iosVersion = "v1.2.5";
}
*/
#pragma mark - 强制更新需要加上这个通知的监听,就是无论怎么打开 ,都会提示更新(有点流氓,慎用)
- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(versionIFUpdata) name:UIApplicationDidBecomeActiveNotification object:nil];
}
 
#pragma mark - 判断版本生是否更新,如有更新,提示用户升级,的方法
-(void)versionIFUpdata
{

    //是否是强制更新
    BOOL force = [_responseObject[@"iosForce"] intValue];
    
    if (force)
    {
        [self forceUpVersion]; //强制
        
    }else
    {
        [self upVersion];  //选择
    }

}
#pragma mark - 强制更新
-(void)forceUpVersion
{

    NSString *iosDownload;
    NSString *iosVersion;
    
    BOOL force  = [_responseObject[@"iosForce"] intValue];
    iosDownload = _responseObject[@"iosDownload"];
//    iosDownload = @"itms-apps://itunes.apple.com/cn/app/wang-yi-yun-yin-le-pao-bufm/id590338362?mt=8";
    iosVersion = _responseObject[@"iosVersion"];
    //更新描述
    NSString *detail = _responseObject[@"iosUpdateMsg"];

    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"" message:[NSString stringWithFormat:@"检测到有新版本:%@ \n该版本是重要版本,不更新将导致应用不可用 \n%@",iosVersion,detail] preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            // itms-apps://itunes.apple.com/cn/app/wang-yi-yun-yin-le-pao-bufm/id590338362?mt=8
            NSURL *url = [NSURL URLWithString:iosDownload];
            [[UIApplication sharedApplication] openURL:url];
            upDateflag = NO;
            
    }];
        
    [alertVC addAction:action1];
    [self presentViewController:alertVC animated:YES completion:nil];
}
#pragma mark - 选择更新
-(void)upVersion
{
    
    
    NSString *iosDownload;
    NSString *iosVersion;
    

    
    iosDownload = _responseObject[@"iosDownload"];
#pragma mark - 注意在这里跳转到APPstore需要URL Schemes :"itms-apps://............."
//    iosDownload = @"itms-apps://itunes.apple.com/cn/app/wang-yi-yun-yin-le-pao-bufm/id590338362?mt=8";
    iosVersion = _responseObject[@"iosVersion"];
    
//去掉其他,只保留数字
iosVersion = [[iosVersion componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet]] componentsJoinedByString:@""];

#pragma  mark -  获取当前本地的版本号
    NSDictionary *localDic =[[NSBundle mainBundle] infoDictionary];
    NSString *localVersion =[localDic objectForKey:@"CFBundleShortVersionString"];
//去掉其他,只保留数字
    localVersion = [[localVersion componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet]] componentsJoinedByString:@""];
//这里需要补齐位数,保证 版本号位数相同的情况下再进行比较 
    if(iosVersion.length != localVersion.length){
        NSInteger gap = 0;
        if(iosVersion.length > localVersion.length){
            gap = iosVersion.length - localVersion.length;
            while (gap) {
                localVersion = [localVersion stringByAppendingString:@"0"];
                gap--;
            }
        }else{
            gap = localVersion.length - iosVersion.length;
            while (gap) {
                iosVersion = [iosVersion stringByAppendingString:@"0"];
                gap--;
            }
        }
    }

  int iosVersionNum = iosVersion.intValue;
  int localVersionNum = localVersion.intValue;
     

    //更新描述
    NSString *detail = _responseObject[@"iosUpdateMsg"];
    if (localVersionNum < iosVersionNum)//如果本地版本比较低 证明要更新版本
    {

        upDateflag = YES;

        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"更新" message:[NSString stringWithFormat:@"检测到有新版本:v%@ \n %@",iosVersion,detail] preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            // itms-apps://itunes.apple.com/cn/app/wang-yi-yun-yin-le-pao-bufm/id590338362?mt=8
            NSURL *url = [NSURL URLWithString:iosDownload];
            [[UIApplication sharedApplication] openURL:url];
            upDateflag = NO;
            
        }];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
            upDateflag = NO;
        }];
        [alertVC addAction:action1];
        [alertVC addAction:action2];
        [self presentViewController:alertVC animated:YES completion:nil];
        
    }

}
//如果是使用AFNetworking 请求的话,需要设置一下返回格式为JSON格式
   AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];

根据@ylyer的反馈,测试了一下 , "http://itunes.apple.com/lookup?id=我的appid&country=cn "这个样子也是可以成功的

上一篇 下一篇

猜你喜欢

热点阅读