zoukankan      html  css  js  c++  java
  • iOS检查App新版本并更新新版本

    检查新版本 更新  第一种方法

      //检查新版本 更新
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            // 耗时的操作
            
            //获取本地版本号
            NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
            NSString *version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
            NSString *build = [infoDictionary objectForKey:@"CFBundleVersion"];
            NSString *nowVersion = [NSString stringWithFormat:@"%@.%@", version, build];
            
            //获取appStore网络版本号
            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@", @"1081299934"]];
            NSString * file =  [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
            
            NSRange substr = [file rangeOfString:@""version":""];
            NSRange range1 = NSMakeRange(substr.location+substr.length,10);
            //    NSRange substr2 =[file rangeOfString:@""" options:nil range:range1];
            NSRange substr2 = [file rangeOfString:@""" options:NSCaseInsensitiveSearch  range:range1];
            NSRange range2 = NSMakeRange(substr.location+substr.length, substr2.location-substr.location-substr.length);
            NSString *appStoreVersion =[file substringWithRange:range2];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                // 更新界面
                
                //如果不一样去更新
                if(![nowVersion isEqualToString:appStoreVersion])
                {
                    
                    [self showAlert];
                    
                }
                
                
            });
        });
    
    
    /**
     *  检查新版本更新
     */
    -(void)showAlert
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"有新的版本啦~~" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"前去更新",nil];
        [alert show];
    
    }
    
    - (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        if(buttonIndex==1)
        {
            // 此处加入应用在app store的地址,方便用户去更新,一种实现方式如下:
            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/us/app/id%@?ls=1&mt=8", @"10812999054"]];
            [[UIApplication sharedApplication] openURL:url];
        }
    }
    View Code

    检查新版本 更新  第二种方法

    // 获取appStore版本号  
    NSString *url = [[NSString alloc] initWithFormat:@"http://itunes.apple.com/lookup?id=%@",@"987953868"];  
      
      
    [self Postpath:url];  
    
    #pragma mark -- 获取数据  
    -(void)Postpath:(NSString *)path  
    {  
          
        NSURL *url = [NSURL URLWithString:path];  
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url  
                                                               cachePolicy:NSURLRequestReloadIgnoringCacheData  
                                                           timeoutInterval:10];  
          
        [request setHTTPMethod:@"POST"];  
          
          
        NSOperationQueue *queue = [NSOperationQueue new];  
          
        [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response,NSData *data,NSError *error){  
            NSMutableDictionary *receiveStatusDic=[[NSMutableDictionary alloc]init];  
            if (data) {  
                  
                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"];  
                }else{  
                      
                    [receiveStatusDic setValue:@"-1" forKey:@"status"];  
                }  
            }else{  
                [receiveStatusDic setValue:@"-1" forKey:@"status"];  
            }  
              
            [self performSelectorOnMainThread:@selector(receiveData:) withObject:receiveStatusDic waitUntilDone:NO];  
        }];  
      
    }  
    
    
    -(void)receiveData:(id)sender  
    {  
        NSLog(@"receiveData=%@",sender);  
          
    }  
    
    最后打印出来的字典中就包含 版本号
    
    receiveData={
        status = 1;
        version = "1.0.0";
    }
    View Code
  • 相关阅读:
    Selenium自动化Chrome浏览器 在windows下窗口最大化
    Linux下的压缩解压缩命令详解及实例
    Windows下及Mac下的IntelliJ IDEA快捷键
    Missing artifact com.oracle:ojdbc6:jar:10.2.0.4.0问题解决 ojdbc包pom.xml出错
    使用cmd命令进行导入
    导入dmp文件时,需要删除原有ORACLE数据库实例
    HTML编码规范、CSS编码规范
    javascript函数与表达式
    JS中闭包、函数与对象的介绍和用法
    JS中typeof和instanceof用法区别
  • 原文地址:https://www.cnblogs.com/ithongjie/p/5331994.html
Copyright © 2011-2022 走看看