[英]iOS Today Extension NSUserDefaults Sharing Data with Containing App using Callbacks
[英]Post NSUserDefaults changes between iOS Keyboard Extension and Containing app?
我以為我已經嘗試了一切並閱讀了該論壇中的所有可能文章。 但是似乎沒有任何作用。 這是一些代碼段和一些設置:
在擴展端:
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”,對象:無)
在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")
...
}
在項目設置上:
. registered "group.request.com" app group to both the extension and containing app
. set "RequestOpenAccess" in NSExtensionAttributes to YES
但是我的DidReceiveRequest函數似乎從未被調用過! 為什么???
請幫忙。
NSNotificationCenter
僅在其運行的進程中起作用。您的鍵盤擴展名和容器應用程序是單獨的進程。 要發布進程間通知,您應該研究CFNotificationCenterGetDarwinNotifyCenter
API調用,該調用返回一個Core Foundation通知中心,該中心可以在進程之間或其他形式的進程間通信中發布通知。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.