ios 版本更新提示

蘋果審核中如果發現項目中有版本更新提示,將禁止上架,那么我們可以讓后臺傳一個字段,上架前后修改一下即可,或者通過下面的方式,判斷當前版本和線上版本的大小,如果當前版本小于線上版本則彈出提示,否則不提示,這樣蘋果審核的時候就不發發現了,同時不需要后臺傳入字段,進行判斷,當然這種方法是已知自己的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];

以上就是版本更新的方法
效果如圖

WechatIMG307.jpeg

3 判斷邏輯放在后臺

我現在公司,在每個請求中加入了版本號這個字段,每次請求都會帶上這個字段,并且每次啟動app都會有借口調用,只要發現當前的版本號不是最新版本號,那么app將自動退出登錄然后彈出警告,這個警告的內容可以后臺配置,這樣前臺幾乎不用做什么事情,負責彈出一個警告框就行

當然,以上都是對于沒有重大bug,不需要強制用戶更新的情況,如果app有重大bug,必須用戶升級才能使用,這時候需要后臺加上一個字段,表明是否需要強制用戶更新,否則用戶無法使用app,這里就不做細致介紹了,加上一個字段,加上一個判斷就行

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容