如何在Today扩展中发送和接收数据 [英] How to send and receive data in Today extensions
问题描述
我想开发一个iOS应用程序,它有一个Widget for notification center,但我不知道如何在View Controller和And Today Extension之间发送和接收数据(传递数据)。
I want to develop an app for iOS that have a Widget for notification center, but I don't know how I should send and receive data (pass data) between View Controller and And Today Extension.
我尝试使用结构,但它不起作用,我也使用了应用程序组,但我不想使用这种方法。
I tried to use structs, but it doesn't work, and also I used app groups but I don't want to use this method.
let shared = NSUserDefaults(suiteName: "group.Demo.Share-Extension-Demo.mahdi")
shared?.setObject("Hello", forKey: "kkk")
推荐答案
除了NSUserDefaults,您可以使用NSNotificationCenter在任何地方发送或接收数据。
Apart from NSUserDefaults, you can use the NSNotificationCenter to send or receive data anywhere.
您需要设置观察者,您可以在其中接收如下数据:
You need to set the observer where you can receive the data like below:
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "dataReceived:", name: "SpecialKey", object: nil)
}
Funciton捕获数据:
Funciton to catch data:
func dataReceived(notification: NSNotification) {
//deal with notification.userInfo
println(notification.userInfo)
println("Received Data")
}
你需要从哪里定义NSNotificationCenter需要发送数据:
And you need to define NSNotificationCenter from where you need to send the data:
NSNotificationCenter.defaultCenter().postNotificationName("SpecialKey", object: nil, userInfo: ["MyData": "Demo"])
参考文献:
希望有所帮助!
http://moreindirection.blogspot.in/2014/08/nsnotificationcenter-swift-and-blocks.html
这篇关于如何在Today扩展中发送和接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!