蘋果審核中如果發(fā)現(xiàn)項目中有版本更新提示菌瘫,將禁止上架虎忌,那么我們可以讓后臺傳一個字段,上架前后修改一下即可萍虽,或者通過下面的方式齐疙,判斷當前版本和線上版本的大小膜楷,如果當前版本小于線上版本則彈出提示,否則不提示贞奋,這樣蘋果審核的時候就不發(fā)發(fā)現(xiàn)了赌厅,同時不需要后臺傳入字段,進行判斷轿塔,當然這種方法是已知自己的app id的情況下才可以的特愿,下面介紹一下直接對比版本號的方法
@implementation UpdateVersion
+ (instancetype)shareVersionUpdateManage {
static UpdateVersion *instance = nil;
staticdispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[selfalloc] init];
});
return instance;
}
- (void)versionUpdate{
//獲得當前發(fā)布的版本
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"];
// 將線上版本和當前版本轉(zhuǎn)化成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:@"有新版本發(fā)布啦!\n%@",updateInfo];
UIAlertView *alertView = [[UIAlertViewalloc]initWithTitle:@"溫馨提示"message:message delegate:selfcancelButtonTitle:@"忽略"otherButtonTitles:@"前往更新",nil];
[alertView show];
}else{
//已是最高版本
NSLog(@"已經(jīng)是最高版本");
}
});
});
}
然后在根控制器中勾缭,調(diào)用一下方法
- (void)checkVersion{
NSString *netWorkingState = [PublicMethod networkingStatesFromStatebar];
if ([netWorkingState isEqual: @"notReachable"]) {
LYLog(@"當前無網(wǎng)絡連接");
}else{
[[CRUpdateVersion shareVersionUpdateManage] versionUpdate];
}
}
2揍障、以上為已上線項目的更新,這樣做有個風險漫拭,很可能會在審核的時候被檢測到有版本更新的代碼亚兄,所以安全起見混稽,還是使用從后臺獲取數(shù)據(jù)比較好
新建一個工具類
@interface JFApplication : NSObject
@property (nonatomic, strong)AFNetworkReachabilityManager* networkManager;
- (void)checkAppVersionOnCompletion:(void (^)(JFVersionResponse *))completion;
// .m中實現(xiàn)
- (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;//版本發(fā)布時間
@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"]
// 最后在根控制器中調(diào)用
[[JFApplication sharedApplication] checkAppVersionOnCompletion:nil];
以上就是版本更新的方法
效果如圖
3 判斷邏輯放在后臺
我現(xiàn)在公司采驻,在每個請求中加入了版本號這個字段,每次請求都會帶上這個字段匈勋,并且每次啟動app都會有借口調(diào)用礼旅,只要發(fā)現(xiàn)當前的版本號不是最新版本號,那么app將自動退出登錄然后彈出警告洽洁,這個警告的內(nèi)容可以后臺配置痘系,這樣前臺幾乎不用做什么事情,負責彈出一個警告框就行
當然饿自,以上都是對于沒有重大bug汰翠,不需要強制用戶更新的情況,如果app有重大bug昭雌,必須用戶升級才能使用复唤,這時候需要后臺加上一個字段,表明是否需要強制用戶更新烛卧,否則用戶無法使用app佛纫,這里就不做細致介紹了,加上一個字段,加上一個判斷就行