作者:lenpre2017 | 来源:互联网 | 2023-08-23 11:19
IamhavingissuescommunicatingbetweenmyhostappanditsextensionsthroughcommunicatingNSUser
I am having issues communicating between my host app and its extensions through communicating NSUserDefaults changes.
我通过传达NSUserDefaults更改在主机应用程序及其扩展之间进行通信时遇到问题。
I initialized NSUserDefaults
using init(suiteName:)
, added KVO observer using the addObserver(...)
method and overrided the method observeValueForKeyPath(...)
but the method observeValueForKeyPath(...)
is not called when I change the value corresponding to the observing key
. It will be greatful if you help me to resolve this.
我使用init(suiteName :)初始化了NSUserDefaults,使用addObserver(...)方法添加了KVO观察者并覆盖了方法observeValueForKeyPath(...)但是当我更改了相应的值时没有调用observeValueForKeyPath(...)方法到观察的关键。如果你帮我解决这个问题会很棒。
PS:here suite name is App groups name and NSUserDefaults
created using suiteName as group identifier
will be inside the private area for app groups.
PS:此处套件名称是应用程序组名称,使用suiteName创建的NSUserDefaults作为组标识符将位于应用程序组的专用区域内。
1 个解决方案
2
The short answer is that you can't use KVO or even NSNotificationCenter on NSUserDefaults
to communicate changes between an App Extension and the containing App.
简短的回答是,您无法在NSUserDefaults上使用KVO甚至NSNotificationCenter来在App扩展和包含应用程序之间进行通信。
There's a great post by Atomic Bird that looks at the ways of coordinating communication. In particular its interesting to look at his analysis of communicating user defaults changes:
Atomic Bird有一篇很棒的文章,着眼于协调沟通的方式。特别有趣的是,看看他对沟通用户默认值变化的分析:
A possible alternative for app/extension notifications is to use the Darwin notification center via CFNotificationCenterGetDarwinNotifyCenter, which actually is a little bit like NSDistributedNotificationCenter. There's some discussion of this by Wade Spires at Apple's dev forums site.
应用/扩展通知的另一种可能选择是通过CFNotificationCenterGetDarwinNotifyCenter使用Darwin通知中心,它实际上有点像NSDistributedNotificationCenter。在Apple的开发论坛网站上,Wade Spires对此进行了一些讨论。
I say "possible" because I'm not 100% confident of this continuing to work. In the documentation for this method, Apple notes that
我说“可能”,因为我对这种继续工作没有百分之百的信心。在这种方法的文档中,Apple注意到了这一点
An application has only one Darwin notification center, so this function returns the same value each time it is called.
应用程序只有一个Darwin通知中心,因此每次调用此函数都会返回相同的值。
So although this is apparently legal, it also sounds a lot like it violates the philosophy inherent in app extension restrictions, viz, that they can't access anything from the hosting app. This is why [UIApplication sharedApplication] is off limits in extensions. I can't help wonder if allowing CFNotificationCenterGetDarwinNotifyCenter is an oversight that might get "fixed" at some point.
因此,尽管这显然是合法的,但它听起来很像违反了应用扩展限制中固有的哲学,即他们无法从托管应用访问任何内容。这就是[UIApplication sharedApplication]在扩展中不受限制的原因。我不禁要问,是否允许CFNotificationCenterGetDarwinNotifyCenter是一种可能在某些时候“固定”的疏忽。
So I guess for now a good solution might be to use MMWormhole as they implement the above solution.
所以我想现在一个好的解决方案可能就是使用MMWormhole来实现上述解决方案。
Your other option is to use to check the user defaults every time your App becomes active and confirm whether any keys have changed, posting the relevant notifications etc.
您的另一个选择是每次您的应用程序处于活动状态时检查用户默认值,并确认是否有任何键已更改,发布相关通知等。
Good luck