您的位置:首页 > 移动开发 > IOS开发

iOS开发 检测版本更新的实现

2016-06-25 11:37 477 查看
苹果给了我们一个接口,能根据应用id请求一些关于应用的信息。我们可以根据返回的信息,来判断版本是否和应用的版本一致,如果不一致,那么就出现新的版本了。这时,就需要向用户提醒有新的版本,需要更新。具体步骤如下:

     NSMutableURLRequest*request=[[NSMutableURLRequestalloc]init];

    

    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",appleID]]];

    

    [request setHTTPMethod:@"GET"];

    

    NSData*returnData=[NSURLConnectionsendSynchronousRequest:requestreturningResponse:nilerror:nil];

    

    NSDictionary*jsonData=[NSJSONSerializationJSONObjectWithData:returnDataoptions:0error:nil];

这里,我们通过同步请求,解析json数据,得到了数据。
好的,我们这里需要,version,trackViewUrl,trackName。

    NSString*latestVersion=[releaseInfoobjectForKey:@"version"];

    

    NSString*trackViewUrl1 =[releaseInfoobjectForKey:@"trackViewUrl"];//地址trackViewUrl

    

    NSString*trackName=[releaseInfoobjectForKey:@"trackName"];//trackName

   获取此应用的版本号

    NSString*currentVersion=[infoDictobjectForKey:@"CFBundleVersion"];

通过latestVersion和currentVersion的比较,来判断是否有新的更新。

    NSDictionary*infoDict=[[NSBundlemainBundle]infoDictionary];

    

    NSString*currentVersion=[infoDictobjectForKey:@"CFBundleVersion"];

    

    doubledoubleCurrentVersion=[currentVersiondoubleValue];

    

    

    

    if(doubleCurrentVersion<doubleUpdateVersion){

        

        

        

        UIAlertView*alert;

        

        alert=[[UIAlertViewalloc]initWithTitle:trackName

               

                                       message:@"有新版本,是否升级!"

               

                                      delegate: self

               

                             cancelButtonTitle:@"取消"

               

                             otherButtonTitles: @"升级",nil];

        

        alert.tag =1001;

        

        [alertshow];

        

    }

    

    else{

        

        UIAlertView*alert;

        

        alert=[[UIAlertViewalloc]initWithTitle:trackName

               

                                       message:@"暂无新版本"

               

                                      delegate: nil

               

                             cancelButtonTitle:@"好的"

               

                             otherButtonTitles: nil,nil];

        

        [alertshow];

        

    }

如果有新的版本,那么就跳转至下载页面,这里就用到了trackViewUrl,trackViewUrl是全路径,直接请求。

 [[UIApplicationsharedApplication]openURL:[NSURLURLWithString:trackViewUrl]];

上面的方法不可以检测最后一位数字的变化下面这种方法可以检测出最后一位数字的变化

        //以"."分隔数字然后分配到不同数组

        NSArray * serverArray = [dict[@"version"]
componentsSeparatedByString:@"."];

        NSArray * localArray = [currentVersion
componentsSeparatedByString:@"."];

        for (int i =
0; i < serverArray.count; i++) {

            

            if ( [serverArray[i]
intValue] > [localArray[i]
intValue]) {

                //有新版本,提示!提示同上

                [self
showUpdateAlert];

                break;

            }

        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: