如何写入AppName-Info.plist [英] How to write to AppName-Info.plist

查看:72
本文介绍了如何写入AppName-Info.plist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我手动向我的AppName-Info.plist添加了一个名为App的密钥,我可以通过调用此代码从中获取值

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 = [NSBundle mainBundle];

NSString *value = [mainBundle objectForInfoDictionaryKey:@"App"];

NSLog(@"App: %@",value);

但我不能做的是用任何代码更改值..是否可能?如果是的话怎么办呢?

But what i couldn't do is changing the value with any code.. is it possible ? and if yes how can it be done ?

提前感谢你的帮助:)

推荐答案

您应该考虑 NSUserDefaults 或者如果你想修改捆绑的.plist,试试这个。

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];
    }
}

这篇关于如何写入AppName-Info.plist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆