1. 程式人生 > >ios 版本更新提示-硬更新/軟更新

ios 版本更新提示-硬更新/軟更新

s實現:

強制更新:每次彈框

非強制更新:一天提示一次

程式碼如下:

步驟一: 將檢測更新寫到APPDelegate中

步驟一: 將檢測更新寫到APPDelegate中
- (void)applicationDidBecomeActive:(UIApplication *)application
{ [JFUpdateVersion upData]; }
步驟二: 完善檢測更新方法
JFUpdateVersion.m

#import "JFUpdateVersion.h" #import "AppDelegate.h" @implementation JFUpdateVersion + (void)upData
{ //當前手機的版本號 NSString *ver = [[[NSBundle mainBundle]infoDictionary] objectForKey:@"CFBundleShortVersionString"]; BaseRequest *request = [BaseRequest sharedBaseRequest]; [request post:G.YXM_act_version otherPar:G.YX_act_version params:@{} success:^(NSURLSessionDataTask *task, id responseObject)
{ if ([responseObject[@"status"]integerValue] == 0) { NSLog(@"%@",responseObject); [[[JFUpdateVersion alloc]init]compareVersionLocalVerson:ver appVerson:responseObject[@"data"][@"version_ios"] andtype:[responseObject[@"data"][@"type"] integerValue] andURl:responseObject[@"data"][@"url"]]
; [UserDefaults setObject:responseObject[@"data"][@"url_ios"] forKey:@"AppURL"]; }else{ } } failure:^(NSURLSessionDataTask *task, NSError *error) { NSLog(@"error:%@",error); }]; }

//步驟三:比較版本號
- (void)compareVersionLocalVerson:(NSString *)localVerson appVerson:(NSString *)appVerson andtype:(NSInteger)type andURl:(NSString *)url
{ //將版本號按照.切割後存入陣列中 NSArray *localArray = [localVerson componentsSeparatedByString:@"."]; NSArray *appArray = [appVerson componentsSeparatedByString:@"."]; NSInteger minArrayLength = MIN(localArray.count, appArray.count); BOOL needUpdate = NO; for(int i=0;i<minArrayLength;i++){//以最短的陣列長度為遍歷次數,防止陣列越界 //取出每個部分的字串值,比較數值大小 NSString *localElement = localArray[i]; NSString *appElement = appArray[i]; NSInteger localValue = localElement.integerValue; NSInteger appValue = appElement.integerValue; if(localValue<appValue) { //從前往後比較數字大小,一旦分出大小,跳出迴圈 needUpdate = YES; break; }else if(localValue>appValue){ needUpdate = NO; break; } } if (needUpdate) { if (type == 1) {//強制更新 [self showForceUpdate]; }else{ if ([IsHadShowUpdate isEqualToString:@"1"]) {//如果已經展示過提示框則不展示了 return; } NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd"]; NSString *nowday = [formatter stringFromDate:[NSDate date]]; NSString *saveDay = [UserDefaults objectForKey:@"versionUpdateKey"]; if (saveDay == nil) { saveDay = @""; } if (![saveDay isEqualToString:nowday]) { //假如不是同一天,更新儲存的日期,並且把flag置為YES [UserDefaults setObject:nowday forKey:@"versionUpdateKey"]; [UserDefaults setObject:@"0" forKey:@"IsHadShowUpdate"]; [self canChooseUpdate]; }else{//如果是同一天的話 [UserDefaults setObject:@"1" forKey:@"IsHadShowUpdate"]; return; } } }else{ } }
//步驟四:強制更新
- (void)showForceUpdate{
    //彈出提示更新彈框
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"親,有新版本了" message:@"為了給您更好的體驗,請點選立即更新!" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"立即更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        NSString *JumpURL = [[NSUserDefaults standardUserDefaults]objectForKey:@"AppURL"];
        
        if(JumpURL.length ==0){
            [JKToast showWithText:@"引數錯誤"];
            return;
        }else{
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:JumpURL]];
            AppDelegate *app = appDelegate;
            UIWindow *window = app.window;
            [UIView animateWithDuration:1.0f animations:^{
                window.alpha = 0;
                window.frame = CGRectMake(0, window.bounds.size.width, 0, 0);
            } completion:^(BOOL finished) {
                exit(0);
            }];
        }
    }];
    [alertVc addAction:action1];
    UIViewController *vc = [UIApplication sharedApplication].delegate.window.rootViewController;
    [vc presentViewController:alertVc animated:YES completion:nil];
}
//步驟五:可選更新
-(void)canChooseUpdate{
    //彈出提示更新彈框
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"親,有新版本了" message:@"更穩定、快速、多彩的功能和體驗,點選立即更新!" preferredStyle:UIAlertControllerStyleAlert];
    //
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        NSString *JumpURL = [[NSUserDefaults standardUserDefaults]objectForKey:@"AppURL"];
        
        if(JumpURL.length ==0){
            [JKToast showWithText:@"引數錯誤"];
            return;
        }else{
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:JumpURL]];
            AppDelegate *app = appDelegate;
            UIWindow *window = app.window;
            
            [UIView animateWithDuration:1.0f animations:^{
                window.alpha = 0;
                window.frame = CGRectMake(0, window.bounds.size.width, 0, 0);
            } completion:^(BOOL finished) {
                exit(0);
            }];
        }
        
    }];
    
    
    
    [alertVc addAction:action2];
    [alertVc addAction:action1];
    
    
    
    UIViewController *vc = [UIApplication sharedApplication].delegate.window.rootViewController;
    [vc presentViewController:alertVc animated:YES completion:nil];
}

轉載請寫明出處。