【问题标题】:Fetching and comparing CFBundleVersion from plist从 plist 中获取和比较 CFBundleVersion
【发布时间】:2014-06-27 13:31:56
【问题描述】:

我正在尝试比较 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";

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

我还尝试了其他方法来比较字符串,但结果始终相同,所以我猜问题在于获取键的值? 任何帮助表示赞赏

【问题讨论】:

  • 你能显示NSLog(@">%@<", appVersion)NSLog(@">%@<", appVer2)的输出吗?
  • 嘿,好吧,所以我添加了NSLog(@"Same Versions!"); / NSLog(@"Different Versions!"); NSLog(@">%@<", appVersion); NSLog(@">%@<", appVer2); 并检查了这个疯狂的输出ghostbin.com/paste/gfwax 最后一个数字显示了一些随机的东西!!!
  • 如果您比较字符串并创建警报之后释放字典会发生什么?
  • 就是这样! ghostbin.com/paste/2mwko 效果很好!谢谢!我认为这也是一些崩溃的原因。请写一个答案,以便我接受:)再次感谢

标签: ios iphone objective-c cocoa-touch jailbreak


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多