iOS 应用程序配置文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8182026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:27:58  来源:igfitidea点击:

iOS Application Configuration File

iosconfiguration-files

提问by António

In a C#application I always used an app.configfile to save some data for my application to load when needed (e.g Connection String).

C#应用程序中,我总是使用一个app.config文件来保存一些数据,以便我的应用程序在需要时加载(例如连接字符串)。

What is the best way to save information like this in an iOS application?

在 iOS 应用程序中保存此类信息的最佳方法是什么?

回答by Rafael Steil

You can create a property list file (there is a template for that in XCode, just to to File -> New file and choose there), so you will have something like "settings.plist", or anything like that. You can think of a plist as being a key => value config file, with the difference that you can also hold arrays and dictionaries, besides plain text as value.

您可以创建一个属性列表文件(在 XCode 中有一个模板,只需到 File -> New file 并在那里选择),因此您将拥有类似“settings.plist”之类的内容。您可以将 plist 视为键 => 值配置文件,不同之处在于除了纯文本作为值之外,您还可以保存数组和字典。

Use NSBundle to load the file in your app, like

使用 NSBundle 在您的应用程序中加载文件,例如

NSString *path = [[NSBundle mainBundle] pathForResource:@"settings" ofType:@"plist"];
NSDictionary *settings = [[NSDictionary alloc] initWithContentsOfFile:path];

You can then access your keys as a regular dictionary. Just don't forget to [release] it after ;).

然后,您可以像普通字典一样访问您的密钥。只是不要忘记在 ;) 之后[发布]它。

回答by Simon

For small bits of data like a connection string, I'd use NSUserDefaults.

对于像连接字符串这样的小数据,我会使用NSUserDefaults

When you want to save your connection string, you'd do this:

当你想保存你的连接字符串时,你可以这样做:

[[NSUserDefaults standardUserDefaults] setObject:myConnectionString 
                                          forKey:@"connectionString"];

When you want to load it, you'd do this:

当你想加载它时,你会这样做:

myConnectionString = [[NSUserDefaults standardUserDefaults] 
                          stringForKey:@"connectionString"];

回答by Jeff

If you're just storing a relatively simple set of configuration stuff, you can simply use NSDictionary's serialization methods (I don't know how to link to the methods directory in the class doc):

如果您只是存储一组相对简单的配置内容,则可以简单地使用NSDictionary的序列化方法(我不知道如何链接到类文档中的方法目录):

NSString *settingsPath = [@"~/pathtoconfig.plist" stringByExpandingTildeInPath];

NSMutableDictionary *settings = [NSMutableDictionary dictionary];
if([[NSFileManager defaultManager] fileExistsAtPath:settingsPath]){
    settings = [NSMutableDictionary dictionaryWithContentsOfFile:settingsPath];
}

// writing settings
[settings writeToFile:settingsPath atomically:YES];

Since I don't know how familiar you are with objective-c, I'll also note:

由于我不知道你对objective-c有多熟悉,我还要注意:

回答by beatsbears

Updated for Swift 3 Xcode 8

为 Swift 3 Xcode 8 更新

Add a new property list to your project named "Sample.plist". Then see the following example.

将一个新的属性列表添加到名为“Sample.plist”的项目中。然后看下面的例子。

    let path: String = Bundle.main.path(forResource: "Sample", ofType: "plist")!
    let sampleConf: NSDictionary = NSDictionary(contentsOfFile: path)!
    print(sampleConf.object(forKey: "test") as! String)

回答by iOSPawan

In iOs every App has own sandbox which will be availble for that app only. So If you want that file to be read only access save it to Application Bundle. else save to Application folder.

在 iOs 中,每个应用程序都有自己的沙箱,仅可用于该应用程序。因此,如果您希望该文件为只读访问权限,请将其保存到应用程序包中。否则保存到应用程序文件夹。

you Can not access Application folder directly So If you want to save your file to that folder you have to do it programmatically.

您无法直接访问 Application 文件夹,因此,如果要将文件保存到该文件夹​​,则必须以编程方式进行。

回答by King-Wizard

iOS Application Configuration File in Swiftwith iOS 8.3:

带有iOS 8.3 的Swift 中的iOS 应用程序配置文件:

    let path: String = NSBundle.mainBundle().pathForResource("Info", ofType: "plist")!
    let nsDictionaryInfoPlist: NSDictionary = NSDictionary(contentsOfFile: path)!

    // Usage example: Google Maps iOS SDK/API Configuration.
    GMSServices.provideAPIKey(nsDictionaryInfoPlist.objectForKey("GoogleMapsiOSAPIKey") as! String)

回答by TheEye

I use property listsfor storing small pieces of data - have a look here on how to use them:

我使用属性列表来存储小块数据 - 请在此处查看如何使用它们:

http://iphoneincubator.com/blog/tag/nspropertylistserialization

http://iphoneincubator.com/blog/tag/nspropertylistserialization

回答by rmeador

I realize I'm late to the party, but after finding this stack overflow I was disheartened at the accepted answer, so I created a podto solve it! This is based on the idea from @RafaelSteil's answer, but it has a simpler interface, caches the loaded config data, and it supports pods providing their own defaults which can be overridden by the main app. I hope it helps someone.

我意识到我迟到了,但是在发现这个堆栈溢出后,我对接受的答案感到沮丧,所以我创建了一个 pod来解决它!这是基于@RafaelSteil's answer的想法,但它有一个更简单的界面,缓存加载的配置数据,并且它支持 pod 提供自己的默认值,可以被主应用程序覆盖。我希望它可以帮助某人。