iOS如何取得APP的版本信息跟Apple服务器对比进行升级提示?

ios中应用的版本判断有两种方法:

1.将你的应用版本号同步在你自己的服务器上,打开程序后去自己的服务器获取版本号和手机上的应用版本号做比较,然后去appstore升级

2.通过url获取appstore上的最新版本信息,然后和手机上的程序版本号做比较,判断是否升级。

最常用的就是方法2,下面讲讲方法2的实现过程。

第一步是去获取appstore上你的应用的版本信息,需要用到的url    #define APP_URL    @"http://itunes.apple.com/lookup?id=662004496"

(替换id即可),我看网上很多的例子都是同步获取信息,这样会阻塞主线程,我还是觉得异步的比较好。

-(void)checkVersion:(NSString* )appurl
{
    ASIFormDataRequest* request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:appurl]];
    [request setRequestMethod:@"POST"];
    [request setDelegate:self];
    [request startAsynchronous];

}

返回结果是需要使用json解析

- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSDictionary* resultDic=[request.responseData JSONValue];
    NSArray* infoArray = [resultDic objectForKey:@"results"];
    if (infoArray.count>0) {
        NSDictionary* releaseInfo =[infoArray objectAtIndex:0];
        NSString* appStoreVersion = [releaseInfo objectForKey:@"version"];
        NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
        NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
        if (![appStoreVersion isEqualToString:currentVersion])
        {
            trackViewURL = [[NSString alloc] initWithString:[releaseInfo objectForKey:@"trackViewUrl"]];
            NSString* msg =[releaseInfo objectForKey:@"releaseNotes"];
            UIAlertView* alertview =[[UIAlertView alloc] initWithTitle:@"版本升级" message:[NSString stringWithFormat:@"%@%@%@", @"新版本特性:",msg, @"
是否升级?"] delegate:self cancelButtonTitle:@"稍后升级" otherButtonTitles:@"马上升级", nil];
            [alertview show];
        }
    
    }
}

最后一步就是点击升级后的跳转界面

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==1)
    {
        [UIApplication sharedApplication] openURL:[NSURL URLWithString:trackViewURL]];
    }
}

这里要讲讲返回的json数据,aapple返回的数据有很多,你看情况选择你需要的数据,一般需要的一个是version,一个是应用新特性releaseNotes,还有一个就是跳转地址 trackViewUrl

    artistId = 
    artistName = 
    artistViewUrl = 
    artworkUrl100 = 
    artworkUrl512 = 
    artworkUrl60 =
    bundleId = 
    contentAdvisoryRating =
    currency = 
    description =
    features =   
    fileSizeBytes = 
    formattedPrice = 
    genreIds =    
    genres =    
    ipadScreenshotUrls =   
    isGameCenterEnabled =
    kind = 
    languageCodesISO2A =   
    price = 0;
    primaryGenreId = 6017;
    primaryGenreName = 
    releaseDate = "2013-08-07T07:03:16Z";
    releaseNotes = "1U3001U6dfbU52a0QQU8054U7cfbU529fU80fd 2U3001U8bfeU7a0bU81eaU52a8U64adU653eU65f6U6e10U9690U8fc7U5ea6Uff0cU89c6U89c9U6548U679cU66f4U597d 3U3001U4e13U9898U6392U7248U91cdU65b0U8bbeU8ba1Uff0cU4f7fU4e4bU66f4U591aU7684U663eU793aU4e13U9898U4ecbU7ecd 4U3001U8f6eU64adU56feU7247U52a0U70b9U6307U793a 5U3001U5b66U9662U4ecbU7ecdU53d8U6210U5bccU6587U672cUff0cU53efU4ee5U6d4fU89c8U5230U56feU7247U63cfU8ff0 6U3001U4feeU6539U4e00U4e9bbug";
    screenshotUrls =   
       
    sellerName = 
    supportedDevices =   
    trackCensoredName = 
    trackContentRating =
    trackId = 
    trackName = 
    trackViewUrl = "https://itunes.apple.com/us/app/quan-min-yu-jia-xue-xiao/id662004496?mt=8&uo=4";
    version = "1.0.1";
    wrapperType =