iOS 版本检测更新

每个正在迭代更新的项目,都必不可少的要检测是否发布了最新的版本,来保证以最优化的版本服务用户。
其实我并不赞成一打开程序就去检测,如果这个程序对我来说可有可无,提示我也不会去更新它,我觉的检测时机可以放在我需要用到这个功能的时候,提示用户更新,那么我相信,这种更新的几率就大很多。言归正传,我们就谈一下具体应该怎么去做。
首先,我们需要知道两个版本号。
1: 苹果商店版本(APP Store)
查询苹果商店版本,我们首先的有一个地址去查询
http://itunes.apple.com/lookup?id = 你的应用程序的ID(有9位有10位)
具体怎么发请求,解析数据,这个童鞋们都明白,我们直接看数据
{
resultCount = 1;
results = (
{
advisories = (
);
artistId = 开发者 ID;
artistName = 开发者名称;
artistViewUrl =
artworkUrl100 =
artworkUrl512 =
artworkUrl60 =
bundleId =
contentAdvisoryRating =
currency =
currentVersionReleaseDate =
description =
features =
fileSizeBytes =
formattedPrice =
genreIds =
genres =
ipadScreenshotUrls =
isGameCenterEnabled =
isVppDeviceBasedLicensingEnabled =
kind =
languageCodesISO2A =
minimumOsVersion =
price =
primaryGenreId =
primaryGenreName =
releaseDate =
releaseNotes = 更新内容
screenshotUrls =
sellerName =
sellerUrl =
supportedDevices =
trackCensoredName =
trackContentRating =
trackId = 应用程序 ID;
trackName =
trackViewUrl =
version = 版本号;
wrapperType =
}
);
}
我们需要的信息一般有 version(版本号) releaseNotes(更新的内容)
2: 正在应用的版本

  NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary];
  NSString* versionNum = [infoDict objectForKey:@"CFBundleVersion"];

我们得到两个版本之后,剩下的事情就是比较了。
现在版本号一般也都是3位,例如:1.0.0 那么我们就不能转换为双精度去比较,我们来试试其他方法:

NSURL *APPUrl = [NSURL URLWithString:GetAppInfoUrl];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:APPUrl]; 
    NSURLSessionDataTask *task =  [session  dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"APP商店信息-------%@",dic);
        int resultCount = [[dic objectForKey:@"resultCount"] intValue];
        if(resultCount > 0){
            NSArray *infoArray = [dic objectForKey:@"results"];
            NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
            NSString *appStoreVersion = [releaseInfo objectForKey:@"version"];
            if(infoArray && releaseInfo && appStoreVersion)
            {
                NSString *note = [releaseInfo objectForKey:@"releaseNotes"];
                NSString *localVerson = [Uilities sharedInstance].getCurrentAppVersion;
                 
                if ([appStoreVersion compare:localVerson options:NSNumericSearch] == NSOrderedDescending){
                    
                    dispatch_async(dispatch_get_main_queue(), ^{
                        UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"有新版本了!赶快更新吧,第一时间体验新功能!"
                                                                     message:note//更新内容
                                                                    delegate:self
                                                           cancelButtonTitle:@"稍后"
                                                           otherButtonTitles:@"更新",nil]; 
                        [av show];
                    }); 
                } 
            }
        }
    }]; 
    [task resume];

这里有个点要提醒一下童鞋们,提示框要回到主线程哦!
如果有更新,那么我们就去更新!

// APPDownloadURL : 项目下载地址
NSURL *url = [NSURL URLWithString:APPDownloadURL];
            [[UIApplication sharedApplication] openURL:url];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,901评论 19 139
  • iOS7开始就添加了应用自动更新,该提示更新适用于用户关闭了自动更新应用功能的情况下,现实中很多iPhone用户都...
    青椒辣不辣阅读 1,589评论 0 1
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,025评论 25 709
  • JohnayXiao阅读 1,072评论 0 0
  • 我愿在一个阳光明媚的上午去拥抱每一个我曾遇见过的人。 ——题记 很感谢上天赐予我的每一段缘份,仿佛是前世我在佛前求...
    伊水_阅读 3,821评论 0 2

友情链接更多精彩内容