[英]iOS Today Extension NSUserDefaults Sharing Data with Containing App using Callbacks
I've got the following problem: 我遇到以下问题:
I created a Today Extension, which contains an UISwitch
. 我创建了Today Extension,它包含一个
UISwitch
。 The IBAction of this switch in Today Extension should store the on-state using the NSUserDefaults
with the initWithSuite like this: Today Extension中此开关的IBAction应该使用
NSUserDefaults
和initWithSuite来存储打开状态,如下所示:
- (IBAction)switchStateChanged:(id)sender {
BOOL isOn = self.preferenceSwitch.isOn;
NSUserDefaults *sharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.x.TodayExtensionSharingDefaults"];
[sharedDefaults setBool: isOn forKey: @"SwitchState"];
[sharedDefaults synchronize];
}
Now in my Containing App, I know that i can access the switch state using this: 现在,在我的“包含应用程序”中,我知道可以使用以下命令访问开关状态:
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.x.TodayExtensionSharingDefaults"];
BOOL value = [defaults boolForKey:@"SwitchState"];
I'm looking for a solution that gives me a callback in my main containing app, when the value of the switch is changed. 我正在寻找一种解决方案,当开关的值更改时,该解决方案可在主包含应用程序中提供回调。
In this solution, i have to set a NSTimer
that refresh the user-defaults every 200ms for example. 在此解决方案中,我必须设置一个
NSTimer
,例如每200ms刷新一次用户默认设置。
Is there any solution by adding an Observer to the sharedDefaults? 通过将Observer添加到sharedDefaults,有什么解决方案吗?
I think this will work for your use case: 我认为这将适合您的用例:
When you leave your app and open up Notification Center, 当您离开应用并打开通知中心时,
- (void)applicationWillResignActive:(UIApplication *)application
is called in your AppDelegate. 在您的AppDelegate中被调用。 When you come back from the widget,
当您从小部件回来时,
- (void)applicationDidBecomeActive:(UIApplication *)application
is called in your AppDelegate. 在您的AppDelegate中被调用。 So just check your value then and fire off a internal notification to your controller.
因此,只需检查您的值,然后向控制器发送内部通知。 You shouldn't have any need for a timer.
您不需要计时器。
Observing the change in your app is not a good solution, even if you could make it work, because you can't be certain that your app is even running when the switch value changes. 即使您可以使应用程序正常工作,但观察它的更改也不是一个好的解决方案,因为当开关值更改时,您不确定应用程序是否正在运行。 It's also not how today extensions are intended to work.
今天的扩展也不打算如此工作。
If your app needs to know the state of this switch the next time it runs (recognizing that it might not be running when the switch is tapped), you're already doing the right thing. 如果您的应用需要在下次运行时知道此开关的状态(确认在轻按该开关时它可能未运行),那么您已经在做正确的事情。
If you need to immediately notify your app that the switch value has changed (again, recognizing that your app might not be running and that doing this might launch your app), you should create a custom URL scheme for your app and then open the URL from the extension. 如果您需要立即通知您的应用程序切换值已更改(再次,请确认您的应用程序可能未运行,并且这样做可能会启动您的应用程序),则应为您的应用程序创建自定义URL方案,然后打开URL从扩展名。 This would be something like:
就像这样:
[NSExtensionContext openURL:completionHandler:]
to open the URL. [NSExtensionContext openURL:completionHandler:]
打开URL。 This will launch your app and pass the provided URL. If the URL scheme is something like mygreatapp
, the app extension would open a URL like mygreatapp:
. 如果该URL方案类似于
mygreatapp
,则应用扩展mygreatapp:
打开一个类似于mygreatapp:
的URL。 You can add detail to the URL if needed, or the app can just use user defaults to look up the saved value. 您可以根据需要在URL中添加详细信息,或者应用程序可以仅使用用户默认值来查找保存的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.