美文网首页
iOS 监测版本更新利用appstore 进行判断

iOS 监测版本更新利用appstore 进行判断

作者: 冯龙胜 | 来源:发表于2017-12-29 15:52 被阅读0次

前言:一般来说很多时候,我们的应用在进行版本更迭的时候,有最新版本时,都是希望能够提醒用户及时更新的。很多时候我们在判读是否有版本更新的时候都是利用后台的接口进行判断,其实对于我们要上到appstore应用市场的应用来说还可以通过监测appstore上一个已经上线版本的版本号进行判断是否需要版本更新。

.h 文件

#import <Foundation/Foundation.h>

@interfaceMonitoringEditionUpdate :NSObject

+ (BOOL)isUpdateVersion;

+ (NSString*)appStoreProjectVersion;

@end

.m 文件

#import"MonitoringEditionUpdate.h"

#define storeAppID @"1234569" // 此处为自己appid 

@implementationMonitoringEditionUpdate

+ (BOOL)isUpdateVersion

{

BOOLisUpdate =NO;

NSString* currentVersion = [selfcurrentProjectVersion];

NSString* appStoreVersion = [selfappStoreProjectVersion];

currentVersion = [currentVersionstringByReplacingOccurrencesOfString:@"."withString:@""];

if (currentVersion.length==2) {

currentVersion= [currentVersionstringByAppendingString:@"0"];

}else if (currentVersion.length==1){

currentVersion= [currentVersionstringByAppendingString:@"00"];

}

appStoreVersion = [appStoreVersionstringByReplacingOccurrencesOfString:@"."withString:@""];

if (appStoreVersion.length==2) {

appStoreVersion= [appStoreVersionstringByAppendingString:@"0"];

}else if (appStoreVersion.length==1){

appStoreVersion= [appStoreVersionstringByAppendingString:@"00"];

}

if([currentVersionfloatValue] < [appStoreVersionfloatValue])

{

isUpdate =YES;

}else{

isUpdate =NO;

}

return isUpdate;

}

// 获取当前项目的版本号

+ (NSString*)currentProjectVersion

{

NSString*currentVersion =@"";

NSDictionary*infoDic=[[NSBundlemainBundle]infoDictionary];

currentVersion= infoDic[@"CFBundleShortVersionString"];//currentVersion为当前工程项目版本号

return currentVersion;

}

// 获取AppStore 版本号

+ (NSString*)appStoreProjectVersion

{

NSString* appStoreVersion =@"";

NSError*error;

NSData*response = [NSURLConnectionsendSynchronousRequest:[NSURLRequestrequestWithURL:[NSURLURLWithString:[NSStringstringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@",storeAppID]]]returningResponse:nilerror:nil];

if (response == nil) {

return nil;

}

NSDictionary*appInfoDic = [NSJSONSerializationJSONObjectWithData:responseoptions:NSJSONReadingMutableLeaveserror:&error];

if (error) {

return nil;

}

NSArray*array = appInfoDic[@"results"];

if (array.count<1) {

return nil;

}

NSDictionary*dic = array[0];

//商店版本号

appStoreVersion = dic[@"version"];

return appStoreVersion;

}

@end

这样 我们在调用的时候

- (void)compareVersion:(UIViewController*)navVC

{

if ([MonitoringEditionUpdateisUpdateVersion]) {

UIAlertController*alercConteoller = [UIAlertControlleralertControllerWithTitle:@"版本有更新"message:[NSStringstringWithFormat:@"检测到新版本(%@),是否更新?",[MonitoringEditionUpdateappStoreProjectVersion]]preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction*actionYes = [UIAlertActionactionWithTitle:@"去更新"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {

NSURL*url = [NSURLURLWithString:[NSStringstringWithFormat:@"https://itunes.apple.com/us/app/id%@?ls=1&mt=8",storeAppID]];

[[UIApplicationsharedApplication]openURL:url];

}];

UIAlertAction*actionNo = [UIAlertActionactionWithTitle:@"暂不更新"style:UIAlertActionStyleCancelhandler:^(UIAlertAction*_Nonnullaction) {

}];

[alercConteolleraddAction:actionYes];

[alercConteolleraddAction:actionNo];

[navVCpresentViewController:alercConteolleranimated:YEScompletion:nil];

}else{

}

}

相关文章

  • iOS 监测版本更新利用appstore 进行判断

    前言:一般来说很多时候,我们的应用在进行版本更迭的时候,有最新版本时,都是希望能够提醒用户及时更新的。很多时候我们...

  • iOS项目版本判断

    我们在开发中经常需要判断当前版本去做相应的操作,比如跳转到AppStore进行更新或判断该App是否第一次打开等。...

  • iOS APP升级跳转到AppStore

    本地检测版本,判断是否需要更新版本。如果需要的话就跳转到AppStore上。首先讲一下如何获取AppStore上的...

  • iOS 版本更新

    需求:当iOS应用迭代更新时,少不了更新提示 思路:通过获取appStore已上传的版本的版本号与手机当前该软件的...

  • 判断IOS版本更新

    //检查版本更新 +(void)checkVersion:(void(^)(NSString*isCurrentV...

  • iOS版本更新判断

    应用每开发一个新版本时,都可能会有新特性介绍页面。所以在应用里就要判断是不是新版本,并且能判断只有第一次进入应用时...

  • flutter实现APP版本更新+(全局弹窗overlay实现)

    升级说明:Android:应用内更新下载+安装。iOS:跳转到appstore下载安装。注:可以通过热更新技术进行...

  • iOS-TestFlight

    TestFlight可以安装iOS上传Appstore构建版本进行测试。第一步:进入App Store Conne...

  • 【iOS】印象笔记8.2.2更新

    今天大象发布了印象笔记 iOS 8.2.2 版,在 AppStore 检查更新即可升级。 这个版本修复了一些 8....

  • 菜鸟教程——iOS启动监测版本更新

    iOS开发当版本迭代时需要提示用户更新,以前基本都是走后台接口监测version字段更新,今天看到了苹果的API接...

网友评论

      本文标题:iOS 监测版本更新利用appstore 进行判断

      本文链接:https://www.haomeiwen.com/subject/priuyxtx.html