iOS-app更新和强制更新

iOS-app更新和强制更新

版本号

规则

版本号的格式:v<主版本号>.<副版本号>.<发布号> 如版本号为2.3.6 1. 我一般把第一位作为大版本号。如出现重大更新,如果用户不更新,这个app都用不下去了。这个时候就要强制用户更新。 2. 第二位作为功能版本号。比如增加了一些新的功能。这个时候通过增加这个版本号,来添加功能。 3. 第三位作为修订版本号。如,上线后出现了一个bug,这个bug需要及时修复,这个时候就增加这个修订版本号。

更新思路

强制更新:

在出现第一位版本号变化的时候,强制更新。通过appStore上的版本和当前的版本对比。

可选更新:

在非第一位版本号变化的时候,用户可以选择暂时忽略(下次还会出现这个弹窗),跳过(下次打开不出现),或者更新app。

具体看后面代码⤵️

效果

强制更新 可选更新

部分代码段

1.获取appstore上的内容

- (void)checkVersion{

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

[manager GET:[LGAppInfo appUrlInItunes] parameters:nil progress:nil success:^(NSURLSessionDataTask *task,id responseObject) {

LGLog(@"%@",responseObject);

//1.是否请求成功

if (((NSArray *)responseObject[@"results"]).count<=0) return;

//2.获取appstore版本号和提示信息

self.storeVersion = [(NSArray *)responseObject[@"results"] firstObject][@"version"];

NSString *releaseNotes = [(NSArray *)responseObject[@"results"] firstObject][@"releaseNotes"];

//3.获取跳过的版本号

NSString *skipVersion = [[NSUserDefaults standardUserDefaults] valueForKey:skipVersionKey];

//4.比较版本号

LGLog(@"%@--%@",self.storeVersion,skipVersion);

if ([self.storeVersion isEqualToString:skipVersion]) {//如果store和跳过的版本相同

return;

}else{

[self compareCurrentVersion:[LGAppInfo appVersion] withAppStoreVersion:self.storeVersion updateMsg:releaseNotes];

}

} failure:nil];

}

2.比较版本

/**

当前版本号和appstore比较

@param currentVersion 当前版本

@param appStoreVersion appstore上的版本

@param updateMsg 更新内容

*/

- (void)compareCurrentVersion:(NSString *)currentVersion withAppStoreVersion:(NSString *)appStoreVersion updateMsg:(NSString *)updateMsg{

NSMutableArray *currentVersionArray = [[currentVersion componentsSeparatedByString:@"."] mutableCopy];

NSMutableArray *appStoreVersionArray = [[appStoreVersion componentsSeparatedByString:@"."] mutableCopy];

if (!currentVersionArray.count ||!appStoreVersionArray.count) return;

//修订版本号

int modifyCount = abs((int)(currentVersionArray.count - appStoreVersionArray.count));

if (currentVersionArray.count > appStoreVersionArray.count) {

for (int index = 0; index < modifyCount; index ++) {

[appStoreVersionArray addObject:@"0"];

}

} else if (currentVersionArray.count < appStoreVersionArray.count) {

for (int index = 0; index < modifyCount; index ++) {

[currentVersionArray addObject:@"0"];

}

}

//大版本必须强制更新<及 第一位表示大版本>

if ([currentVersionArray.firstObject integerValue] > [appStoreVersionArray.firstObject integerValue]) {

//强制更新---

[self showUpdateAlertMust:YES withStoreVersion:appStoreVersion message:updateMsg];

}else{//不需要强制更新 检查后面的版本号,如果比appstore大 则更新

for (int index = 0; index

if ([currentVersionArray[index] integerValue]> [appStoreVersionArray[index] integerValue]) {

[self showUpdateAlertMust:NO withStoreVersion:appStoreVersion message:updateMsg];

return;

}

}

}

}

3.跳转界面

/**

打开appstore 执行更新操作

*/

- (void)openAppStoreToUpdate{

LGLog(@"打开到appstore");

NSURL *trackUrl = [NSURL URLWithString:self.trackViewUrl];

if ([LGApplication canOpenURL:trackUrl]) {

[[UIApplication sharedApplication] openURL:trackUrl];

}

}

使用方法

在appdelegate.m中,程序唤醒后调用如下:

- (void)applicationDidBecomeActive:(UIApplication *)application {

[[LGCheckVersion shareCheckVersion] checkVersion];

}

demo地址

https://github.com/LGLee/LGCheckUpdate.git

点个赞送大胸美女一枚

相关推荐