Jaume
Jaume

Reputation: 923

iOS able to read plist file on simulator but not on device

Previously I read posts with same problem but my question persists. My app needs to read firstly a plist file to get some parameters and, when loaded on device, is unable to read default settings plist (working on simulator). One solution should be copy that plist into documents directory, but only this one? Is device unable to read user defaults set in plist? if I copy it to docs directory, will device be able to associate keys for plist if has another location? I need to read user defaults before executing any function. Thank you.

-(void)loadSettings
{    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    self.userID = [[NSUserDefaults standardUserDefaults] stringForKey:@"userID"];

    self.nom2 = [[NSUserDefaults standardUserDefaults] stringForKey:@"t2"];
    self.nom3 = [[NSUserDefaults standardUserDefaults] stringForKey:@"t3"];
    self.nom4 = [[NSUserDefaults standardUserDefaults] stringForKey:@"t4"];

    self.currentGlobal = [[NSUserDefaults standardUserDefaults] stringForKey:@"versionApp"];
    NSLog(@"Version App = %@", currentGlobal);


    self.colorTab = [[NSUserDefaults standardUserDefaults] stringForKey:@"color"];

    firstBoot = [[NSUserDefaults standardUserDefaults] boolForKey:@"firstBoot"];

    [defaults synchronize];

}

Upvotes: 0

Views: 665

Answers (3)

Dan Fairaizl
Dan Fairaizl

Reputation: 2170

It sounds like you just want to set some initial values in user defaults so your app can read them in. If that is the case then you'll simply want to use the registerDefaults: method of NSUserDefaults. Example:

[[NSUserDefaults standardUserDefaults] registerDefaults:
    [NSDictionary dictionaryWithObject: [NSNumber numberWithBool:YES] 
                                forKey: @"firstBoot"]];

The beauty of NSUserDefaults is you never have to mess with pList stuff or the iOS filesystem.

Upvotes: 3

picciano
picciano

Reputation: 22701

You do no need to copy the plist for the standardUserDefaults to the documents directory. You should let iOS create this file on its own and not include it with your app. If you need to set defaults, then you should use the registerDefaults: method on the standardUserDefaults instance to do so by passing in a dictionary of key-value pairs.

Upvotes: 1

Lou Franco
Lou Franco

Reputation: 89162

Have you looked at using NSUserDefaults?

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html

It automatically does what you are describing and you don't need to manage files. It's a simple API that you can read and write settings to and iOS takes care of storage for you.

Upvotes: 1

Related Questions