我正在尝试比较 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
我得到以下错误结果:
如果我手动设置 NSString:
NSString *appVersion =@"2.11.8";
NSString *appVer2 =@"2.11.8";
我得到了正确的预期结果:
我也尝试了其他方法来比较字符串,但结果总是一样,所以我猜问题出在获取键的值上? 感谢任何帮助
最佳答案
我已经习惯了 ARC,以至于我不再 100% 确定 MRC 规则了。但我假设
您要么必须保留
字典中的值appVersion
和appVer2
,
或者,将 [dict release]
推迟到不再需要这些值之后。
由于您不拥有从字典中获取的值,因此如果
词典发布。
(如果您使用 ARC 编译,这不会成为问题!)
备注:从字典中获取值的指定方法是objectForKey:
。
valueForKey:
在许多情况下也适用,但可以不同。它应该只被使用
用于键值编码魔法。
关于ios - 从 plist 中获取和比较 CFBundleVersion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23570884/