开发者

Editing an external Propery List (.plist)

Here is my code:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application

}

- (IBAction)unlockIt:(id)sender {

    if ([[appField stringValue] length] < 1) {
        [status setTextColor:[NSColor redColor]];
        [status setStringValue:@"error with name"];
    }
    else {
        [status setStringValue:@""];
        [status setTextColor:[NSColor greenColor]];

        NSString *path = [NSString stringWithFormat:@"/Applications/%@.app/Contents", [appField stringValue]];

        NSBundle *bundle = [NSBundle bundleWithPath:path]; 

        NSString *infoPlist = [bundle pathForResource:@"Info" ofType:@"plist"];

        NSString *randomIdenti开发者_开发技巧fier = [NSString stringWithFormat:@"com.derp.%i", arc4random()];

        [infoPlist setValue:randomIdentifier forKey:@"Bundle identifier"];

        [status setStringValue:@"attempt successful"];
    }
}

I get this error:

[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Bundle identifier.

How can I fix this?


The key isn’t ‘Bundle identifier’ — that’s the friendly representation of the key.

The actual key is ‘CFBundleIdentifier’, and there’s a CFStringRef constant for it that you can cast to an NSString:

(NSString *)kCFBundleIdentifierKey

Edit: More issues with your code:

NSString *infoPlist = [bundle pathForResource:@"Info" ofType:@"plist"];
[infoPlist setValue:randomIdentifier forKey:@"Bundle identifier"];

infoPlist is a string containing that file path of the Info.plist file inside an application bundle. It is not the plist itself. You need to read Info.plist into a dictionary, change its contents, and then write it again. For example:

NSString *infoPlistPath = [bundle pathForResource:@"Info" ofType:@"plist"];
NSMutableDictionary *infoPlist = [NSDictionary dictionaryWithContentsOfFile:infoPlistPath];
[infoPlist setObject:randomIdentifier forKey:(NSString *)kCFBundleIdentifierKey];
[infoPlist writeToFile:infoPlistPath atomically:YES];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜