【版本更新】iOS自动跳转到App Store方法

以下写法不完整

#pragma mark 版本更新
-(void)doRenewVersion
{
    
    //判断是否最新版本,否,则跳转到App Store更新
    //获取当前版本
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *appCurVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    
    /*
    //设置请求头
    //os:判断客户端类型
    NSString * os_Head = nil;
    
    //判断是iPhone还是iPad,iPhone:2,iPad:4
    //判断iPhone还是iPad
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        //iPhone
        os_Head = @"2";
    }else{
        //ipad
        os_Head = @"4";
        
    }
    */
    
    //获取最新版本
    
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    
    //[manager.requestSerializer setValue:os_Head forHTTPHeaderField:@"os"];
    
//此处URL_VERSION为本人工程内请求版本号及App Store地址的URL,实际请求自行调整
    [manager POST:URL_VERSION parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //版本号
        NSString * app_Version = responseObject[@"version"];
        //App Store跳转链接
        NSString * urlStr = responseObject[@"download_url"];
        
        if (![appCurVersion isEqualToString:app_Version]) {
            
//跳转到App Store
            NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
            [_webView loadRequest:request];
            //或者(两种方法都可以跳转到App Store):
/*
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/shan-shan-pen-di-fu-nu-jian/id1049660516?mt=8"]];
*/
            
        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        BSGLOG(@"版本更新网络请求错误:%@",error);
    }];
    
    
}

更新版本号判断方法

  • 考虑到上架更新版本时间不固定,有可能出现App上架而后台请求的版本号还未更新,出现App当前版本比后台的最新版本还高德情况

/**判断版本号,是否需要更新,当需要更新返回YES*/
+(BOOL)BSGWithAppVersion:(NSString *)appVersion LastVersion:(NSString *)lastVersion
{
    //应用版本
    NSArray * appVArray = [appVersion componentsSeparatedByString:@"."];
    //最新版本
    NSArray * lastVArray = [lastVersion componentsSeparatedByString:@"."];
    
    NSInteger count = appVArray.count;
    //判断是否是最新
    BOOL isLastVersion = YES;
    
    NSInteger appVNum;
    NSInteger lastVNum;
    for (int i = 0; i < count; i++) {
        
        appVNum  = [appVArray[i] integerValue];
        lastVNum = [lastVArray[i] integerValue];
        if (appVNum < lastVNum) {
            isLastVersion = NO;
            break;
        }
        
    }
    
    return isLastVersion;
}

使用方法:

//应用版本
    NSString * appVersion = @"3.9.78";
//后台保存最新版本
    NSString * lastVersion = @"3.9.786";
   
    BOOL isLast = [BSGCommonMethod BSGWithAppVersion:appVersion LastVersion:lastVersion];

你可能感兴趣的:(【版本更新】iOS自动跳转到App Store方法)