简体   繁体   English

iOS Today扩展NSUserDefaults使用回调与包含应用程序共享数据

[英]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: 就像这样:

  • In the app, declare a custom URL scheme in the "URL types" section of the app's "Info" settings. 在应用程序中,在应用程序的“信息”设置的“ URL类型”部分中声明自定义URL方案。
  • Also in the app, add code to the app delegate to receive requests to open URLs. 同样在应用程序中,向应用程序代理添加代码以接收打开URL的请求。
  • In the app extension, use [NSExtensionContext openURL:completionHandler:] to open the URL. 在应用扩展中,使用[NSExtensionContext openURL:completionHandler:]打开URL。 This will launch your app and pass the provided URL. 这将启动您的应用并传递提供的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.

相关问题 如何使用NSUserDefaults与包含应用程序共享数据? - 今日Extension Widget - How to share data with containing app using NSUserDefaults ? - Today Extension Widget 在Today Extension(iOS)中设置NSUserDefaults - Setting NSUserDefaults in Today Extension (iOS) 发布NSUserDefaults在iOS键盘扩展和包含应用之间进行更改吗? - Post NSUserDefaults changes between iOS Keyboard Extension and Containing app? 使用NSUserDefaults与Today Extension(小部件)共享一组自定义对象 - Sharing an array of custom objects with Today Extension (widget) with NSUserDefaults 在iOS App和Todays Extension之间共享数据 - Sharing Data Between iOS App and Todays Extension iOS - 通知今天扩展主应用程序中的核心数据更改 - iOS - Notify today extension for Core Data changes in the main app iOs - 如何使用url将数据从Widget(今日扩展)传递到App? - iOs - How to pass data from Widget (Today Extension) to App with url? 无法将NSUserDefaults中的数据检索到今天的扩展名(Swift) - Cannot retrieve data from NSUserDefaults into today extension (Swift) NSUserDefaults for Today Extension不保存数据,iOS很快。 在这里找不到我在做什么错 - NSUserDefaults for Today Extension not saving data, iOS swift. Can't figure out what I am doing wrong here 带有核心数据的 iOS 今日扩展 - iOS today extension with core data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM