ios - 从 plist 中获取和比较 CFBundleVersion

标签 ios iphone objective-c cocoa-touch jailbreak

我正在尝试比较 com.apple.mobile.installation.plist 中 2 个应用程序的 CFBundleVersion 键,其中包括 iPhone 上每个已安装应用程序的信息

NSString *appBundleID =@"net.someapp.app";
NSString *appBundleID2=@"net.someapp.app2";

NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:
         @"/var/mobile/Library/Caches/com.apple.mobile.installation.plist"];
NSDictionary *User = [dict valueForKey:@"User"];

//get first app version
NSDictionary *bundleID = [User valueForKey:appBundleID];
NSString *appVersion = [bundleID valueForKey:@"CFBundleVersion"];

//get second app version
NSDictionary *bundleID2 = [User valueForKey:appBundleID2];
NSString *appVer2 = [bundleID2 valueForKey:@"CFBundleVersion"];

[dict release];
 
if ([appVersion isEqualToString:appVer2]) {
     NSString *str1=[NSString stringWithFormat:@"Original Version: %@",appVersion];
     NSString *str2=[NSString stringWithFormat:@"2nd Version: %@",appVer2];
     NSString *msg=[NSString stringWithFormat:@"%@\n%@",str1,str2];
     UIAlertView* alertView = [[UIAlertView alloc]
           initWithTitle:@"Same Versions!" message:msg delegate:nil
       cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alertView show]; 
}
else {
     NSString *str1=[NSString stringWithFormat:@"Original Version: %@",appVersion];
     NSString *str2=[NSString stringWithFormat:@"2nd Version: %@",appVer2];         
     NSString *msg=[NSString stringWithFormat:@"%@\n%@",str1,str2];
     UIAlertView* alertView = [[UIAlertView alloc]
         initWithTitle:@"Different Versions!" message:msg delegate:nil
            cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alertView show]; 
}

目前两个应用的版本都设置为2.11.8

我得到以下错误结果: Wrong Result

如果我手动设置 NSString:

NSString *appVersion =@"2.11.8";
NSString *appVer2 =@"2.11.8";

我得到了正确的预期结果:

Correct Result

我也尝试了其他方法来比较字符串,但结果总是一样,所以我猜问题出在获取键的值上? 感谢任何帮助

最佳答案

我已经习惯了 ARC,以至于我不再 100% 确定 MRC 规则了。但我假设 您要么必须保留字典中的值appVersionappVer2, 或者,将 [dict release] 推迟到不再需要这些值之后。 由于您不拥有从字典中获取的值,因此如果 词典发布。

(如果您使用 ARC 编译,这不会成为问题!)

备注:从字典中获取值的指定方法是objectForKey:valueForKey: 在许多情况下也适用,但可以不同。它应该只被使用 用于键值编码魔法。

关于ios - 从 plist 中获取和比较 CFBundleVersion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23570884/

相关文章:

ios - 如何在 iOS 8 中获取第三方键盘大小?

ios - -[NSDictionary initWithObjects :forKeys:]: count of objects (0) differs from count of keys (2)

ios - 我想使用非消耗品和自动更新那么,是否可以在一个应用程序中使用 2 个 IAP 产品?

ios - UserDefault 总是返回 nil

ios - 无法更改 UIImageView 的图像

ios - 如何创建自定义图层类

ios - 构建聊天应用程序时应该使用 UITableView 还是 UICollectionView?

objective-c - drawRect 中的 UILabel 不绘制

ios - ABPeoplePickerNavigationController predicateForEnablingPerson - 街道地址的谓词格式是什么?

ios - NSFetchedResultsController - 与部分名称不同的排序顺序