简体   繁体   English

发布NSUserDefaults在iOS键盘扩展和包含应用之间进行更改吗?

[英]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.

相关问题 iOS Today扩展NSUserDefaults使用回调与包含应用程序共享数据 - iOS Today Extension NSUserDefaults Sharing Data with Containing App using Callbacks NSUserDefaults不在iOS 8中的应用程序和自定义键盘之间存储值 - NSUserDefaults not storing values between app and custom keyboard in iOS 8 如何在iOS App包含扩展和扩展(不是主机应用程序)之间进行通信 - How to communicate between iOS App Containing Extension and Extension (not Host App) 使用钥匙串在 iOS 扩展程序及其包含的应用程序之间共享? - Share between an iOS extension and its containing app with the keychain? 如何使用NSUserDefaults与包含应用程序共享数据? - 今日Extension Widget - How to share data with containing app using NSUserDefaults ? - Today Extension Widget iOS 键盘扩展(应用组) - iOS Keyboard Extension(App Groups) Swift键盘扩展程序更新NSUserDefaults - Swift Keyboard Extension update NSUserDefaults iOS 8自定义键盘扩展程序不允许与包含的应用程序共享数据吗? - iOS 8 Custom Keyboard extension don't allow share data with containing app? 为什么NSUserDefaults不能在我的应用和共享扩展程序之间工作? - Why doesn't NSUserDefaults work between my app & share extension? 如何创建/使用共享应用程序组容器作为包含应用程序及其在ios中的扩展名之间的缓存存储 - How to create/use a shared app group container as cache storage between containing app and it's extension in ios
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM