简体   繁体   中英

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

}

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM