How to write to AppName-Info.plist

前端 未结 3 1615
情歌与酒
情歌与酒 2021-01-16 13:23

i added a key called \"App\" to my AppName-Info.plist manually , i can get the value from it by calling this code

NSBundle *mainBundle;
mainBundle = [NSBundl         


        
3条回答
  •  执笔经年
    2021-01-16 13:53

    You should consider NSUserDefaults or if you want to modify a bundled .plist try this.

        NSString* plistFilePath = nil;
    NSFileManager* manager = [NSFileManager defaultManager];
    if ((plistFilePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"mySpecial/PathTo.plist"])) 
    {
        if ([manager isWritableFileAtPath:plistFilePath]) 
        {
            NSMutableDictionary* infoDictioio = [NSMutableDictionary dictionaryWithContentsOfFile:plistFilePath];
            [infoDictio setObject:@"foo object" forKey:@"fookey"];
            [infoDictio writeToFile:plistFilePath atomically:NO];
            [manager setAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] ofItemAtPath:[[NSBundle mainBundle] bundlePath] error:nil];
        }
    }
    

提交回复
热议问题