iOS-检查版本更新

水深无声 2023-10-16 23:58 95阅读 0赞

1、

// FIR.im的自动更新

  1. NSString *bundleId = [[NSBundlemainBundle]infoDictionary][@"CFBundleIdentifier"];
  2. NSString *bundleIdUrlString = [NSStringstringWithFormat:@"http://api.fir.im/apps/latest/%@?api_token=71eb982be4f093719d2b7d985b236fb7&type=ios", bundleId];
  3. NSURL *requestURL = [NSURLURLWithString:bundleIdUrlString];
  4. NSURLRequest *request = [NSURLRequestrequestWithURL:requestURL];
  5. [NSURLConnectionsendAsynchronousRequest:requestqueue :[NSOperationQueuecurrentQueue]completionHandler:^(NSURLResponse *response,NSData *data,NSError *connectionError) {
  6. if (connectionError) {
  7. //do something
  8. XNLog(@"版本检查失败");
  9. }else {
  10. NSError *jsonError =nil;
  11. id object = [NSJSONSerializationJSONObjectWithData:dataoptions:0error:&jsonError];
  12. if (!jsonError && [objectisKindOfClass:[NSDictionaryclass]]) {
  13. //do something
  14. //NSString * version=object[@"version"]; //对应 CFBundleVersion,对应Xcode项目配置"General"中的 Build
  15. NSString * versionShort=object[@"versionShort"];//对应 CFBundleShortVersionString,对应Xcode项目配置"General"中的 Version
  16. //NSString * localVersion=[[NSBundle mainBundle] infoDictionary][@"CFBundleVersion"];
  17. NSString * localVersionShort=[[NSBundlemainBundle]infoDictionary][@"CFBundleShortVersionString"];
  18. nss_updateURL=object[@"update_url"];//如果有更新需要用Safari打开的地址
  19. //NSString *changelog=object[@"changelog"]; //如果有更新需要用Safari打开的地址
  20. if (![localVersionShort isEqualToString:versionShort]) {
  21. [[AlertHandlersingleton]showConfirm_default_title:@"发现新版本"message:[NSStringstringWithFormat:@"发现新版本v%@",versionShort]delegate:self
  22. cancelButtonTitle:@"忽略"otherButtonTitle:@"点击更新"tag:1];
  23. }
  24. }
  25. }
  26. }];

2、

  1. <span style="font-size:18px;">if ([[CommFunsingleton]event_updateVersion] ==YES) {
  2. UIApplication *application = [UIApplicationsharedApplication];
  3. [applicationopenURL:[NSURLURLWithString:[UesrInfosingleton].updateURL]];
  4. }else{
  5. NSString * localVersionShort=[[NSBundlemainBundle]infoDictionary][@"CFBundleShortVersionString"];
  6. [[AlertHandlersingleton]showConfirm_default_title:[NSStringstringWithFormat:@"当前版本v%@", localVersionShort]message:@"当前已是最新版本"delegate:selfcancelButtonTitle:@"确定"otherButtonTitle:niltag:1];
  7. }
  8. -(BOOL)event_updateVersion{
  9. // TODO:去掉版本检查(苹果审核不通过)
  10. [UesrInfosingleton].needUpdate =NO;
  11. returnNO;
  12. [UesrInfosingleton].needUpdate =YES;
  13. NSString *bundleId = [[NSBundlemainBundle]infoDictionary][@"CFBundleIdentifier"];
  14. NSString *bundleIdUrlString = [NSStringstringWithFormat:@"http://api.fir.im/apps/latest/%@?api_token=71eb982be4f093719d2b7d985b236fb7&type=ios", bundleId];
  15. NSURL *requestURL = [NSURLURLWithString:bundleIdUrlString];
  16. NSURLRequest *request = [NSURLRequestrequestWithURL:requestURL];
  17. [NSURLConnectionsendAsynchronousRequest:requestqueue :[NSOperationQueuecurrentQueue]completionHandler:^(NSURLResponse *response,NSData *data,NSError *connectionError) {
  18. if (connectionError) {
  19. //do something
  20. XNLog(@"版本检查失败");
  21. }else {
  22. NSError *jsonError =nil;
  23. id object = [NSJSONSerializationJSONObjectWithData:dataoptions:0error:&jsonError];
  24. if (!jsonError && [objectisKindOfClass:[NSDictionaryclass]]) {
  25. //do something
  26. //NSString * version=object[@"version"]; //对应 CFBundleVersion,对应Xcode项目配置"General"中的 Build
  27. NSString * versionShort=object[@"versionShort"];//对应 CFBundleShortVersionString,对应Xcode项目配置"General"中的 Version
  28. //NSString * localVersion=[[NSBundle mainBundle] infoDictionary][@"CFBundleVersion"];
  29. NSString * localVersionShort=[[NSBundlemainBundle]infoDictionary][@"CFBundleShortVersionString"];
  30. NSString *nss_updateURL=object[@"update_url"];//如果有更新需要用Safari打开的地址
  31. //NSString *changelog=object[@"changelog"]; //如果有更新需要用Safari打开的地址
  32. if (![localVersionShort isEqualToString:versionShort]){
  33. [UesrInfosingleton].needUpdate =YES;
  34. }else{
  35. [UesrInfosingleton].needUpdate =NO;
  36. }
  37. [[UesrInfosingleton]setUpdateURL:nss_updateURL];
  38. }
  39. }
  40. }];
  41. return [UesrInfosingleton].needUpdate;
  42. }
  43. -(void)setUpdateURL:(NSString *)updateURL{
  44. NSMutableDictionary *updateURLDic = [[NSMutableDictionaryalloc]initWithObjectsAndKeys:updateURL,@"updateURL",nil];
  45. [PlistReaderdoWrite:@"updateURL"dic:updateURLDic];
  46. }</span>

发表评论

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

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

相关阅读

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

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