質問

I'm trying to compare CFBundleVersion key of 2 Apps inside com.apple.mobile.installation.plist which include the info of every installed application on 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]; 
}

The version of both apps is currently set to 2.11.8

I am getting the following wrong result: Wrong Result

If i set the NSString manually:

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

i get the correct desired result:

Correct Result

I also tried other ways to compare the strings but the result was always the same, so i guess the problem is with fetching the values of the keys? Any help is appreciated

役に立ちましたか?

解決

I am so used to ARC that I am not 100% sure about the MRC rules anymore. But I assume that you either have to retain the values appVersion and appVer2 from the dictionary, or alternatively, postpone the [dict release] until after the values are no longer needed. Since you don't own the values fetched from the dictionary, they become invalid if the dictionary is released.

(This would not be a problem if you compile with ARC!)

Remark: The designated method to get a value from a dictionary is objectForKey:. valueForKey: works also in many cases, but can be different. It should only be used for Key-Value Coding magic.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top