Я работаю с NSUserDefaults
в своем приложении для iPhone, и по какой-то причине приложение должно быть запущено/возобновлено дважды, чтобы изменения в настройках вступили в силу. Соответствующий код:Почему для iOS NSDefaults требуется два запуска для вступления в силу?
- (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.
Почему это займет запуск приложения дважды для изменения параметров вступили в силу?
Вы пытались вызвать '-synchronize', чтобы попытаться правильно установить значения? – kevboh