概述
#pragma mark - 检查版本更新
+(void)checkVersion {
//
if (![self judgeNeedVersionUpdate]) {
//
return;
//
}
NSString *localVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
NSString *urlStr = @"https://itunes.apple.com/cn/lookup?id=xxx"; //xxx-你的Appid,iTunes可查看
[[HDNetworking sharedHDNetworking] GET:urlStr parameters:nil success:^(id
_Nonnull responseObject) {
int code = [responseObject[@"resultCount"] intValue];
if (code == 1) {
NSArray *sourceArray = responseObject[@"results"];
if (sourceArray.count >= 1) {
//AppStore内最新App的版本号
NSDictionary *sourceDict = sourceArray[0];
NSString *newVersion = sourceDict[@"version"];
if ([self judgeNewVersion:newVersion withOldVersion:localVersion])
{
UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"版本更新" message:sourceDict[@"releaseNotes"] preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"暂不更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"去更新" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
//跳转到AppStore下载界面
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:sourceDict[@"trackViewUrl"]]];
}];
[alertVc addAction:action1];
[alertVc addAction:action2];
[[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:alertVc animated:YES completion:nil];
}
}
}
} failure:^(NSError * _Nonnull error) {
}];
}
//每天进行一次版本判断
- (BOOL)judgeNeedVersionUpdate {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSString *dateString = [formatter stringFromDate:[NSDate date]];
NSString *currentDate = [UserDefaultsUtils getCurrentDate];
if ([currentDate isEqualToString:dateString]) {
return NO;
}
[UserDefaultsUtils setCurrentDate:dateString];
return YES;
}
/*!
@brief 判断当前app版本和AppStore最新app版本大小
@param newVersion AppStore版本号
@param oldVersion 本地APP版本号
@return YES为AppStore版本大于本地APP版本
*/
- (BOOL)judgeNewVersion:(NSString *)newVersion withOldVersion:(NSString *)oldVersion {
NSArray *newArray = [newVersion componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"."]];
NSArray *oldArray = [oldVersion componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"."]];
if (oldArray.count == newArray.count) {
for (NSInteger i = 0; i < newArray.count; i ++) {
if ([newArray[i] integerValue] > [oldArray[i] integerValue]) {
return YES;
} else if ([newArray[i] integerValue] < [oldArray[i] integerValue]) {
return NO;
}
}
}
return NO;
}
最后
以上就是时尚小馒头为你收集整理的iOS 怎么在app里提示版本更新的全部内容,希望文章能够帮你解决iOS 怎么在app里提示版本更新所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复