Я работаю с NSUserDefaults
в своем приложении для iPhone, и по какой-то причине приложение необходимо запустить / возобновить дважды, чтобы изменения в настройках вступили в силу. Соответствующий код:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
DLog(@"Registered default user defaults values.");
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"DefaultUserDefaults" ofType:@"plist"]];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
...
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
DLog(@"rescheduling notifications...");
[self rescheduleAllNotifications];
}
- (void)rescheduleAllNotifications {
//
// Wipe the slate clean by cancelling all local notifications
//
[[UIApplication sharedApplication] cancelAllLocalNotifications];
//
// Only reschedule notifications if the user prefers to have them scheduled
//
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults boolForKey:@"notifications_enabled_preference"]) {
DLog(@"Notifications enabled. processing now...");
...
Итак, вот мой вывод отладчика (начиная с notifications_enabled_preference
, установленного на YES
):
[Session started at 2011-01-31 14:25:58 -0600.]
AppDelegate application:didFinishLaunchingWithOptions:] Registered default user defaults values.
AppDelegate applicationDidBecomeActive:] rescheduling notifications...
AppDelegate rescheduleAllNotifications] Notifications enabled. processing now...
-> Switch to Settings and turn notifications_enabled_preference to NO, then re-launch the app
AppDelegate applicationDidBecomeActive:] rescheduling notifications...
AppDelegate rescheduleAllNotifications] Notifications enabled. processing now...
-> Click home screen button, then re-launch the app **again**
AppDelegate applicationDidBecomeActive:] rescheduling notifications...
AppDelegate rescheduleAllNotifications] Notifications disabled. Skipping notification processing.
Почему для того, чтобы измененные настройки вступили в силу, требуется запустить приложение дважды ?