[英]Post NSUserDefaults changes between iOS Keyboard Extension and Containing app?
I thought I have tried everything and read every possible articles in this forum. 我以为我已经尝试了一切并阅读了该论坛中的所有可能文章。 But nothing seems to work. 但是似乎没有任何作用。 Here is some code snippet and some settings: 这是一些代码段和一些设置:
On Extension side: 在扩展端:
let thisDefaults = NSUserDefaults(suiteName: "group.request.com")
thisDefaults?.setObject("Hello", forKey: "prevWord")
thisDefaults?.setObject("World", forKey: "word")
let success = thisDefaults?.Synchronize()
NSNotificationCenter.defaultCenter().postNotificationName("ExtensionRequestChanges", object: nil) NSNotificationCenter.defaultCenter()。postNotificationName(“ ExtensionRequestChanges”,对象:无)
On Containingg app side: 在Containingg应用程序方面:
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("DidReceiveRequest:", name: "ExtensionRequestChanges",
object: nil)
func DidReceiveRequest(notification: NSNotification) {
// Read defaults
let thisDefaults = NSUserDefaults(suiteName: "group.request.com")
let word = thisDefaults?.stringForKey("word")
let prevWord = thisDefaults?.stringForKey("prevWord")
...
}
On the project settings: 在项目设置上:
. registered "group.request.com" app group to both the extension and containing app
. set "RequestOpenAccess" in NSExtensionAttributes to YES
But my DidReceiveRequest function never seemed to get called! 但是我的DidReceiveRequest函数似乎从未被调用过! Why??? 为什么???
Please help. 请帮忙。
NSNotificationCenter
only works in the process it's running in. Your keyboard extension and container app are separate processes. NSNotificationCenter
仅在其运行的进程中起作用。您的键盘扩展名和容器应用程序是单独的进程。 To post interprocess notifications, you should investigate the CFNotificationCenterGetDarwinNotifyCenter
API call, which returns a Core Foundation notification center that can post notifications between processes, or other forms of interprocess communication. 要发布进程间通知,您应该研究CFNotificationCenterGetDarwinNotifyCenter
API调用,该调用返回一个Core Foundation通知中心,该中心可以在进程之间或其他形式的进程间通信中发布通知。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.