苹果审核中如果发现项目中有版本更新提示,将禁止上架,那么我们可以让后台传一个字段,上架前后修改一下即可,或者通过下面的方式,判断当前版本和线上版本的大小,如果当前版本小于线上版本则弹出提示,否则不提示,这样苹果审核的时候就不发发现了,同时不需要后台传入字段,进行判断,当然这种方法是已知自己的app id的情况下才可以的,下面介绍一下直接对比版本号的方法
@implementation UpdateVersion
+ (instancetype)shareVersionUpdateManage {
static UpdateVersion *instance = nil;
staticdispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[selfalloc] init];
});
return instance;
}
- (void)versionUpdate{
//获得当前发布的版本
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
//耗时的操作---获取某个应用在AppStore上的信息,更改id就行
NSString *string = [NSStringstringWithContentsOfURL:[NSURLURLWithString:@"http://itunes.apple.com/lookup?id=00000000000"]encoding:NSUTF8StringEncodingerror:nil];
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
// 判断是否取到信息
if (![data isKindOfClass:[NSDataclass]]) {
return ;
}
NSDictionary *dic = [NSJSONSerializationJSONObjectWithData:data options:NSJSONReadingMutableLeaveserror:nil];
//获得上线版本号
NSString *version = [[[dic objectForKey:@"results"]firstObject]objectForKey:@"version"];
NSString *updateInfo = [[[dic objectForKey:@"results"]firstObject]objectForKey:@"releaseNotes"];
//获得当前版本
NSString *currentVersion = [[[NSBundlemainBundle]infoDictionary]objectForKey:@"CFBundleShortVersionString"];
// 将线上版本和当前版本转化成int型
int versionInt = [[version stringByReplacingOccurrencesOfString:@"."withString:@""] intValue];
int currentVersionInt = [[currentVersion stringByReplacingOccurrencesOfString:@"."withString:@""] intValue]; dispatch_async(dispatch_get_main_queue(), ^{
//更新界面
NSLog(@"线上版本:%d 当前版本:%d",versionInt,currentVersionInt);
if (versionInt > currentVersionInt) {
//有新版本
NSString *message = [NSStringstringWithFormat:@"有新版本发布啦!\n%@",updateInfo];
UIAlertView *alertView = [[UIAlertViewalloc]initWithTitle:@"温馨提示"message:message delegate:selfcancelButtonTitle:@"忽略"otherButtonTitles:@"前往更新",nil];
[alertView show];
}else{
//已是最高版本
NSLog(@"已经是最高版本");
}
});
});
}
然后在根控制器中,调用一下方法
- (void)checkVersion{
NSString *netWorkingState = [PublicMethod networkingStatesFromStatebar];
if ([netWorkingState isEqual: @"notReachable"]) {
LYLog(@"当前无网络连接");
}else{
[[CRUpdateVersion shareVersionUpdateManage] versionUpdate];
}
}
2、以上为已上线项目的更新,这样做有个风险,很可能会在审核的时候被检测到有版本更新的代码,所以安全起见,还是使用从后台获取数据比较好
新建一个工具类
@interface JFApplication : NSObject
@property (nonatomic, strong)AFNetworkReachabilityManager* networkManager;
- (void)checkAppVersionOnCompletion:(void (^)(JFVersionResponse *))completion;
// .m中实现
- (void)checkAppVersionOnCompletion:(void (^)(JFVersionResponse *))completion {
if (_checkingVersion) {
return;
}
_checkingVersion = YES;
__weak typeof(self) weakSelf = self;
JFVersionRequest *request = [[JFVersionRequest alloc] init];
[[JFNetworkManager sharedManager] post:request forResponseClass:[JFVersionResponse class] success:^(WXResponse *response) {
_checkingVersion = NO;
[weakSelf handleVersionResponse:(JFVersionResponse *)response];
if(completion){
completion((JFVersionResponse *)response);
}
} failure:^(NSError *error) {
_checkingVersion = NO;
if (completion) {
completion(nil);
}
}];
}
- (void)handleVersionResponse:(JFVersionResponse *)response {
if ([response success]) {
JFVersion * version = response.data;
if([JFVersion isNewVersion:version.version]) {
NSMutableString *message = [NSMutableString string];
for (int i = 0;i < version.releaseLog.count;i++) {
NSString *item = version.releaseLog[i];
[message appendString:item];
if (i != version.releaseLog.count - 1) {
[message appendString:@"\n"];
}
}
[[WXAlert sharedAlert] showConfirmMessage:message withTitle:kStr(@"New Version") cancelButtonTitle:kStr(@"Cancel") okButtonTitle:kStr(@"Download") onCompletion:^(NSInteger buttonIndex, UIAlertView *alertView) {
if (buttonIndex == 1) {
kOpenURL(version.releaseUrl);
}
}];
}
}
}
2.2 所需的模型类
#import "JFResponse.h"
#import "JFVersion.h"
@interface JFVersionResponse : JFResponse
@property(nonatomic,strong)JFVersion *data;
@end
#import "JFVersionResponse.h"
@implementation JFVersionResponse
@end
#import "WXObject.h"
@interface JFVersion : WXObject
@property(nonatomic,copy)NSString *version;//版本号
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *releaseTime;//版本发布时间
@property(nonatomic,copy)NSString *releaseUrl;//版本获取地址
@property(nonatomic,strong)NSArray *releaseLog;//版本简要日志
+ (BOOL) isNewVersion:(NSString *)version;
@end
#import "JFVersion.h"
@implementation JFVersion
+ (BOOL) isNewVersion:(NSString *)version {
if([NSString isNullOrEmpty:version]){
return NO;
}
NSArray *versions = [version splitBy:@"."];
if(!versions || versions.count==0){
return NO;
}
NSArray *currentVerions = [kAppVersion splitBy:@"."];
if(!currentVerions || currentVerions.count==0){
return NO;
}
for(int i=0,n=(int)MIN(versions.count, currentVerions.count);i<n;i++){
int v = [[currentVerions objectAtIndex:i] getIntValue];
int v2 = [[versions objectAtIndex:i] getIntValue];
if(v<v2){
return YES;
}
if(v>v2) {
return NO;
}
}
if (versions.count>currentVerions.count) {
for (NSInteger i=currentVerions.count; i<versions.count; i++) {
int v = [[versions objectAtIndex:i] getIntValue];
if (v>0) {
return YES;
}
}
}
return NO;
}
@end
- (NSArray*) splitBy:(NSString *)splitString{
return [self componentsSeparatedByString: splitString];
}
#define kAppVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
// 最后在根控制器中调用
[[JFApplication sharedApplication] checkAppVersionOnCompletion:nil];
以上就是版本更新的方法
效果如图
3 判断逻辑放在后台
我现在公司,在每个请求中加入了版本号这个字段,每次请求都会带上这个字段,并且每次启动app都会有借口调用,只要发现当前的版本号不是最新版本号,那么app将自动退出登录然后弹出警告,这个警告的内容可以后台配置,这样前台几乎不用做什么事情,负责弹出一个警告框就行
当然,以上都是对于没有重大bug,不需要强制用户更新的情况,如果app有重大bug,必须用户升级才能使用,这时候需要后台加上一个字段,表明是否需要强制用户更新,否则用户无法使用app,这里就不做细致介绍了,加上一个字段,加上一个判断就行