zoukankan      html  css  js  c++  java
  • iOS基础(六)——获取当前版本,比较更新版本

    1、版本的比较

    //处理版本
    -(void)VersonUpdate{
        //定义的app的地址
        NSString *urld = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",@"你的id"];
        
        //网络请求app的信息,主要是取得我说需要的Version
        NSURL *url = [NSURL URLWithString:urld];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                               cachePolicy:NSURLRequestReloadIgnoringCacheData
                                                           timeoutInterval:10];
        [request setHTTPMethod:@"POST"];
        
        NSURLSession *session = [NSURLSession sharedSession];
        
        NSURLSessionDataTask *task = [session dataTaskWithURL:url 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];
    }
    

    2、获取自身版本号

    -(void)receiveData:(id)sender
    {
        //获取APP自身版本号 localVersion 例如:0.2.1 -> 021
        NSString *localVersion = [[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleShortVersionString"];
        
        localVersion = [localVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
        
        NSString * serverVerison = sender[@"version"];
        
        serverVerison = [serverVerison stringByReplacingOccurrencesOfString:@"." withString:@""];
        if ([serverVerison intValue]>[localVersion intValue]) {
            [self updateVersion];
        }
    }
    

    3、提示更新

    -(void)updateVersion{
        NSString *msg = [NSString stringWithFormat:@"更新最新版本,优惠信息提前知"];
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"升级提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"现在升级"style:UIAlertActionStyleDestructive handler:^(UIAlertAction*action) {
            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/cn/app/id1355602256?mt=8"]];
            [[UIApplication sharedApplication]openURL:url];
        }];
        [alertController addAction:otherAction];
        [self.window.rootViewController presentViewController:alertController animated:YES completion:nil];//@"1355602256"
    }
    

    此方法是要上架后才可以用!

    欢迎关注我的公众号:

  • 相关阅读:
    错误记录(一):VSCode
    【洛谷P4770】你的名字
    【洛谷P3177】树上染色
    【洛谷P3704】数字表格
    【CF762F】Tree nesting
    【洛谷P5064】等这场战争结束之后
    【洛谷P3346】诸神眷顾的幻想乡
    【BZOJ#2119】股市的预测
    UiPath数据抓取Data Scraping的介绍和使用
    UiPath录制器的介绍和使用
  • 原文地址:https://www.cnblogs.com/smileK/p/9554212.html
Copyright © 2011-2022 走看看