【问题标题】:How can I update the NSUserDefaults from the Settings Bundle while the app is running in background?当应用程序在后台运行时,如何从 Settings Bundle 更新 NSUserDefaults?
【发布时间】:2013-11-04 21:29:38
【问题描述】:

在设置包中,我有一个多选选项。因此,当我选择某些内容时,应用程序应该接收新值并使用它来启动 UILocalNotification。我试过的,只有在我打开应用程序时才会更新值。

  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions


{  
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(defaultsChanged:)
                                                 name:NSUserDefaultsDidChangeNotification
                                               object:nil];

}
- (void)applicationDidEnterBackground:(UIApplication *)application
{

  [[NSUserDefaults standardUserDefaults]synchronize];
}

【问题讨论】:

  • 你的问题不清楚。如果“应用程序在后台运行”,它如何接收新值?为什么需要defaultsChanged: 选择器?
  • 还请澄清您所说的“在后台运行”是什么意思,它可能意味着几件事,例如播放音频、提供 GPS 或 VOIP,或执行即将到期的任务。您可能还指“暂停”
  • 我正在使用选择器查看用户是否更改了设置包中的任何选项。暂停是我的意思。
  • 我猜您在启动时轮询 NSUserDefaults,并在此阶段应用设置。当用户从 Settings.app 中更改设置时,如果检测到这些更改并将其应用于 NSUserDefaults,那么您的应用在下次启动之前不会知道。
  • 好的,谢谢。因此,无法在下次启动时通知应用。

标签: ios objective-c nsuserdefaults uilocalnotification settings.bundle


【解决方案1】:

如果您不是真正在后台运行,而是suspended,那么您的应用在被用户唤醒或重新启动之前不会收到任何事件。如果您在后台运行,则需要轮询设置包中的更改并做出相应的反应。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 2012-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多