IOS版本更新代码——商店版本

桃扇骨 2023-10-16 23:42 116阅读 0赞

#define kStoreAppId @”1064298982” //商店的苹果appid

  1. -(void)checkAppUpdate
  2. {
  3. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  4. NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
  5. NSString *nowVersion = [infoDict objectForKey:@"CFBundleVersion"];
  6. NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@", kStoreAppId]];
  7. NSString * file = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
  8. NSLog(@"%@",file);
  9. dispatch_async(dispatch_get_main_queue(), ^{
  10. NSData *data = [file dataUsingEncoding:NSUTF8StringEncoding];
  11. NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
  12. if(((NSArray*)dic[@"results"]).count>0)
  13. {
  14. NSString *newVersion = [NSString stringWithFormat:@"%@",dic[@"results"][0][@"version"]];
  15. NSString *nowVersion2 = [nowVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
  16. NSString *newVersion2 = [newVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
  17. if([newVersion2 intValue]>[nowVersion2 intValue])
  18. {
  19. NSString *newMsg = [NSString stringWithFormat:@"%@",dic[@"results"][0][@"releaseNotes"]];
  20. NSString *newstr = [NSString stringWithFormat:@"有新版V%@",newVersion];
  21. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:newstr message:newMsg delegate:self cancelButtonTitle:@"取消"otherButtonTitles:@"更新",nil];
  22. [alert show];
  23. }
  24. }
  25. });
  26. });
  27. }
  28. - (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  29. {
  30. if(buttonIndex==1)
  31. {
  32. // 此处加入应用在app store的地址,方便用户去更新,一种实现方式如下:
  33. NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/us/app/id%@?ls=1&mt=8", kStoreAppId]];
  34. [[UIApplication sharedApplication] openURL:url];
  35. }
  36. }

发表评论

表情:
评论列表 (有 0 条评论,116人围观)

还没有评论,来说两句吧...

相关阅读

    相关 IOS学习之如何判断版本更新

    在我们使用应用时,一打开应用,如果此应用有新的版本,常常能在应用中给出提示,是否要更新此应用。所以,我们就来看看,版本更新是如何实现的。 应用 苹果给了我们一个接口,能