虽然苹果官方是不允许应用自动检测更新,提示用户下载,因为苹果会提示你有多少个软件需要更新,但是有的时候提示用户一下有新版还是很有必要的。

 

首先说一下原理:

每个上架的苹果应用程序,都会有一个应用程序的ID,根据这个ID我们就可以获取到当前程序的最新版本号,然后和自己的版本号作比较,如果一样的话就是最新版,反之就不是新版,就可以提示用户来手动下载最新版的程序。因为有ID所以就可以定位到这个APP,点击下载即可。

 

源码:

一般建议检测更新的代码放到主页控制器里。

首先还要导入一个头文件用来打开AppStore下载更新

//AppStore#import 

接着还有代理

SKStoreProductViewControllerDelegate

然后开始检测更新

    //检测版本,版本更新    NSError *error;    NSString *urlStr = @"http://itunes.apple.com/lookup?id=上架AppID";    NSURL *url = [NSURL URLWithString:urlStr];    NSURLRequest *request = [NSURLRequest requestWithURL:url];    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];    NSDictionary *appInfoDict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];    if (error)    {        return;    }    NSArray *resultArray = [appInfoDict objectForKey:@"results"];     if (![resultArray count])    {        return;    }        NSDictionary *infoDict = [resultArray objectAtIndex:0];    //获取服务器上应用的最新版本号    NSArray* arr=[infoDict[@"version"] componentsSeparatedByString:@"."];    NSInteger updateVersion=0;       for (int i=0; i

 

接着就是用户更不更的问题了

 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {        if (alertView.tag == 1112427256)    {        if (buttonIndex == 1)        {            //点击”升级“按钮,就从打开app store上应用的详情页面            SKStoreProductViewController *storeProductVC = [[SKStoreProductViewController alloc] init];            storeProductVC.delegate = self;            NSDictionary *dict = [NSDictionary dictionaryWithObject:@"上架AppID" forKey:SKStoreProductParameterITunesItemIdentifier];            [storeProductVC loadProductWithParameters:dict completionBlock:^(BOOL result, NSError *error)             {                 if (result)                 {                     [self presentViewController:storeProductVC animated:YES completion:nil];                 }             }];        }    }}

还有就是用户打开AppStore但是没有下载就返回回来的状况

- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController{    [viewController dismissViewControllerAnimated:YES completion:nil];}

OK到这里就结束了。这样的话就可以检测App是不是最新版了,而且用户也能实时看到,最关键的是苹果审核还能通过。