1

In my App, I am trying to implement SettingView by NSUserDefault. So, I wrote program code as follows:

Method example of saving user setting:

+ (void)setUserAutoPlaySetting:(BOOL)setting {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; 
    [userDefaults setBool:setting forKey:kAutoPlaySettingKey];
    [userDefaults synchronize];
}

This method is received user setting as BOOL and saves by NSUserDefault. I confirmed my App save user setting on simulator but not on real Device. In addition, this method is enable to save NO but disable to save YES on a real device. Can anyone please tell how can I overcome this problem? Thanks.

awksp
  • 11,764
  • 4
  • 37
  • 44
  • 1
    This code should work on a real device, please do some more debugging to see where the real problem lies. – rckoenes May 19 '14 at 13:10
  • 1
    Should work. What is the code you are using the check that it doesn't work? – YogevSitton May 19 '14 at 13:11
  • Provide the failing code including the declaration for `kAutoPlaySettingKey`.. – zaph May 19 '14 at 13:17
  • 1
    Note from Apple docs on `synchronize`: "Because this method is automatically invoked at periodic intervals, use this method only if you cannot wait for the automatic synchronization". I the general case `synchronize` does not need to be called. – zaph May 19 '14 at 13:20
  • 4
    The advantage of manually calling `synchronize` is that it you don't get an automatic save if the app is killed (`SIGKILL` or crash). That matters for really critical data, but it also matters during development. It's really frustrating when you stop your app in Xcode and it fails to store the settings (I generally just wait a few seconds rather than adding `synchronize`, but I understand the desire to manually do it). – Rob Napier May 19 '14 at 13:24
  • What do you mean by `SettingView` ? Are you trying to implement `Settings Bundle` ? – Abhishek Bedi May 19 '14 at 15:12

0 Answers0