ios - 在开发时在App捆绑中以编程方式编辑plist

标签 ios objective-c xcode plist

我在XCode上创建了一个plist,它将具有一些无法手动插入的值。因此,我想在开发时以编程方式添加此值。但是似乎我只能读取plist,但我无法保存App捆绑包中的plist,这在运行时很有意义。.当我分发我的应用程序时,我希望每个人都拥有这个plist文件,这就是为什么我不保存在文档或缓存上。我怎样才能实现自己想要的?

最佳答案

通过http://www.karelia.com/cocoa_legacy/Foundation_Categories/NSFileManager__Get_.m(下面粘贴),您可以使用在那里找到的-(NSString *) pathFromUserLibraryPath:(NSString *)inSubPath方法在用户的个人库中构建路径。

例如,NSString *editedPlist = [self pathFromUserLibraryPath:@"my.plist"];为您提供用户库中经过修改的plist的名称(即使该plist尚不存在)。

根据您拥有哪种类型的plist来读取/写入它,但是您可以使用以下方法将其读入字典中:

NSMutableDictionary *thePlist= [[NSMutableDictionary alloc] initWithContentsOfFile:editedPlist ];

如果您无法阅读(例如,通过[thePlist count] == 0轻易检测到),则可以调用同一initWithContentsOfFile:初始化程序,并使用捆绑包中模板的路径,但随后将plist写出到editedPlist路径中,以便它出现在用户目录。

这是我上面引用的实用程序方法:
/*
    NSFileManager: Get the path within the user's Library directory
    Original Source: <http://cocoa.karelia.com/Foundation_Categories/NSFileManager__Get_.m>
    (See copyright notice at <http://cocoa.karelia.com>)
*/

/*" Return the path in the user library path of the given sub-path.  In other words, if given inSubPath is "foo", the path returned will be /Users/myUser/Library/foo
"*/
-  (NSString *) pathFromUserLibraryPath:(NSString *)inSubPath
{
    NSArray *domains = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES);
    NSString *baseDir= [domains objectAtIndex:0];
    NSString *result = [baseDir stringByAppendingPathComponent:inSubPath];
    return result;
}

关于ios - 在开发时在App捆绑中以编程方式编辑plist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23721505/

相关文章:

iphone - UIImage 元数据

ios - 框架的 Xcode 8 链接器错误,架构 i386 的 undefined symbol

ios - UISegmentedControl 在 vi​​ewLoad 上选择了 tintColor

ios - 如果应用程序直接从后台启动而不是从通知消息启动,如何知道推送消息信息?

ios - SLComposeViewController 在 ios7 中将图像发布到 facebook 上

ios - 如何立即替换呈现的 UIViewController?

iphone - 在不重复代码的情况下执行初始化的最佳方法?

objective-c - NSOutlineView + NSTreeController + CoreData + Bindings 行重复

iphone - 如何更改 UINavigationBar 中的编辑/完成按钮标题

ios - 在 Swift 中开发的 iOS 应用程序中的滑动选项卡布局