从plist中获取和比较CFBundleVersion

时间:2014-05-09 17:40:36

标签: 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

我也尝试过比较字符串的其他方法,但结果总是一样的,所以我想问题是获取键的值? 任何帮助表示赞赏

1 个答案:

答案 0 :(得分:1)

我已经习惯了ARC,我不再对MRC规则100%肯定。但我想 要么必须retain字典中的值appVersionappVer2, 或者,将[dict release]推迟到不再需要值之后。 由于您不拥有从字典中获取的值,因此它们将变为无效 字典发布。

(如果用ARC编译,这将成为一个问题!)

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