iOS检测版本更新 避免审核失败

之前appstore会自动提示更新 最近两次不知道为什么不提示了 那没办法 后台也没提供对比接口 自己去判断一下当前版本和appstore版本吧

代码 appdidfinishluanching里调用

- (void)versionCheck
{
    NSString *string = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://itunes.apple.com/lookup?id=1057544694"] encoding:NSUTF8StringEncoding error:nil];
    
    if (string!=nil &&[string length]>0&&[string rangeOfString:@"version"].length==7) {
        [self checkAppUpdate:string];
    }
}
- (void)checkAppUpdate:(NSString *)appInfo
{
    NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    NSString *appInfo1 = [appInfo substringFromIndex:[appInfo rangeOfString:@"\"version\":"].location+10];
    appInfo1 = [[appInfo1 substringToIndex:[appInfo1 rangeOfString:@","].location] stringByReplacingOccurrencesOfString:@"\"" withString:@""];
    if (![appInfo1 isEqualToString:version]) {
        NSLog(@"新版本:%@,当前版本:%@",appInfo1,version);
        UIAlertView *arelt = [[UIAlertView alloc] initWithTitle:@"" message:[NSString stringWithFormat:@"新版本%@已发布!",appInfo1] delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
        [arelt addButtonWithTitle:@"前往更新"];
        [arelt show];
    }
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==1) {
        NSString *url = @"https://itunes.apple.com/cn/app/man-man-da-chao-ren-qi-er/id1057544694?mt=8";
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
    }
}

上面方法我猜会审核失败 我又改成了下面的方法 审核时候比对的版本肯定是没有appstore版本高 所以不会弹出提示 用户使用时候 版本是比appstore低 会弹出提示

- (void)versionCheck
{
    
    //定义的App地址
    NSString *urls = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=1057544694"];
    //AppID即是如图红色箭头获取的AppID
    //PS:有的时候可能会请求不到数据,但是AppID对了,有可能是App是上架区域范围的原因,建议使用在com末尾加上“/cn”
    //例:NSString *url = [NSString stringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@",AppID];
    
    
    //网络请求App的信息(我们取Version就够了)
    NSURL *url = [NSURL URLWithString:urls];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestReloadIgnoringCacheData
                                                       timeoutInterval:10];
    
    [request setHTTPMethod:@"POST"];
    
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSMutableDictionary *receiveStatusDic=[[NSMutableDictionary alloc]init];
        if (data) {
            
            //data是有关于App所有的信息
            NSDictionary *receiveDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            if ([[receiveDic valueForKey:@"resultCount"] intValue]>0) {
                
                [receiveStatusDic setValue:@"1" forKey:@"status"];
                [receiveStatusDic setValue:[[[receiveDic valueForKey:@"results"] objectAtIndex:0] valueForKey:@"version"]   forKey:@"version"];
                
                //请求的有数据,进行版本比较
                [self performSelectorOnMainThread:@selector(receiveData:) withObject:receiveStatusDic waitUntilDone:NO];
            }else{
                
                [receiveStatusDic setValue:@"-1" forKey:@"status"];
            }
        }else{
            [receiveStatusDic setValue:@"-1" forKey:@"status"];
        }
        
    }];
    
    [task resume];
    
    
   
}
-(void)receiveData:(id)sender
{
    //获取APP自身版本号
    NSString *localVersion = [[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleShortVersionString"];
    
    NSArray *localArray = [localVersion componentsSeparatedByString:@"."];
    NSArray *versionArray = [sender[@"version"] componentsSeparatedByString:@"."];
    
    
    if ((versionArray.count == 3) && (localArray.count == versionArray.count)) {
        
        if ([localArray[0] intValue] <  [versionArray[0] intValue]) {
            [self updateVersion];
        }else if ([localArray[0] intValue]  ==  [versionArray[0] intValue]){
            if ([localArray[1] intValue] <  [versionArray[1] intValue]) {
                [self updateVersion];
            }else if ([localArray[1] intValue] ==  [versionArray[1] intValue]){
                if ([localArray[2] intValue] <  [versionArray[2] intValue]) {
                    [self updateVersion];
                }
            }
        }
    }
}
- (void)updateVersion
{
   
        UIAlertView *arelt = [[UIAlertView alloc] initWithTitle:@"" message:[NSString stringWithFormat:@"新版本已发布!"] delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
        [arelt addButtonWithTitle:@"前往更新"];
        [arelt show];
    
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==1) {
        NSString *url = @"https://itunes.apple.com/cn/app/man-man-da-chao-ren-qi-er/id1057544694?mt=8";
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
    }
}

你可能感兴趣的:(iOS检测版本更新 避免审核失败)