iOS Application Configuration File

后端 未结 8 1477
南笙
南笙 2021-01-31 16:13

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

Wha

相关标签:
8条回答
  • 2021-01-31 16:42

    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):

    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:

    • In iOS, the home directory refers to the application's sandboxed directory.
    • I recommend reading a little bit up on reference counting. Although objective-c, as of iOS5, now automatically reference counts for you, there are some edge cases to be aware about (Here's a SO question on ARC)
    0 讨论(0)
  • 2021-01-31 16:45

    Updated for Swift 3 Xcode 8

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

        let path: String = Bundle.main.path(forResource: "Sample", ofType: "plist")!
        let sampleConf: NSDictionary = NSDictionary(contentsOfFile: path)!
        print(sampleConf.object(forKey: "test") as! String)
    
    0 讨论(0)
  • 2021-01-31 16:49

    iOS Application Configuration File in Swift with iOS 8.3:

        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)
    
    0 讨论(0)
  • 2021-01-31 16:50

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

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

    0 讨论(0)
  • 2021-01-31 16:53

    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.

    Use NSBundle to load the file in your app, like

    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 ;).

    0 讨论(0)
  • 2021-01-31 16:55

    For small bits of data like a connection string, I'd use 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"];
    
    0 讨论(0)
提交回复
热议问题