在哪里最好使用 Watch Connectivity 调用 updateApplicationContext?
Where best to call updateApplicationContext using Watch Connectivity?
几篇详细介绍 Watch Connectivity 的优秀博文 (http://www.kristinathai.com/watchos-2-tutorial-using-application-context-to-transfer-data-watch-connectivity-2/ and http://natashatherobot.com/watchconnectivity-application-context/) 使用简单的应用程序示例,当您点击 iPhone 上的 UI 时,这些示例会向手表发送数据。
我的应用只是列出了来自我的 iPhone 应用的数据,所以我不需要立即发送数据,我只是想在应用加载或进入后台时发送它...为此我已经在 didFinishLaunching
和 didEnterBackground
中创建了 updateApplicationContext
...但是我的手表界面控制器中的数据源委托非常容易被触发...特别是 glance 只加载到模拟器,从不在设备上。有没有更好的时间和地点来推送信息?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
WatchSessionManager.sharedManager.startSession()
do {
try WatchSessionManager.sharedManager.updateApplicationContext(["peopleDict" : peopleDict])
} catch {
print(error)
}
return true
}
func applicationDidEnterBackground(application: UIApplication) {
do {
try WatchSessionManager.sharedManager.updateApplicationContext(["peopleDict" : peopleDict])
} catch {
print(error)
}
}
下面是我的 WatchSessionManager
我曾经在 extensionDelegate
的 appliciationDidFinishLaunching
中调用 activiateSession
import WatchConnectivity
protocol DataSourceChangedDelegate {
func dataSourceDidUpdate(dataSource: DataSource)
}
class WatchSessionManager: NSObject, WCSessionDelegate {
static let sharedManager = WatchSessionManager()
private override init() {
super.init()
}
private var dataSourceChangedDelegates = [DataSourceChangedDelegate]()
private let session: WCSession = WCSession.defaultSession()
func startSession() {
session.delegate = self
session.activateSession()
}
func addDataSourceChangedDelegate<T where T: DataSourceChangedDelegate, T: Equatable>(delegate: T) {
dataSourceChangedDelegates.append(delegate)
}
func removeDataSourceChangedDelegate<T where T: DataSourceChangedDelegate, T: Equatable>(delegate: T) {
for (index, indexDelegate) in dataSourceChangedDelegates.enumerate() {
if let indexDelegate = indexDelegate as? T where indexDelegate == delegate {
dataSourceChangedDelegates.removeAtIndex(index)
break
}
}
}
}
// MARK: Application Context
// use when your app needs only the latest information
// if the data was not sent, it will be replaced
extension WatchSessionManager {
// Receiver
func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) {
dispatch_async(dispatch_get_main_queue()) { [weak self] in
self?.dataSourceChangedDelegates.forEach { [=11=].dataSourceDidUpdate(DataSource(data: applicationContext))}
}
}
}
由于 updateApplicationContext
只存储最新的应用程序上下文,您可以随时更新它。手表只会获取最新的数据。没有包含旧上下文的队列。
在手表端,激活会话和设置 WCSessionDelegate 的最安全位置是在 ExtensionDelegate
init
方法中:
class ExtensionDelegate: NSObject, WKExtensionDelegate {
override init() {
super.init()
WatchSessionManager.sharedManager.startSession()
}
...
}
您的 Glance 没有更新,因为当显示 Glance 时,applicationDidFinishLaunching
没有被调用(因为当只启动 Glance 时手表应用程序没有启动)
几篇详细介绍 Watch Connectivity 的优秀博文 (http://www.kristinathai.com/watchos-2-tutorial-using-application-context-to-transfer-data-watch-connectivity-2/ and http://natashatherobot.com/watchconnectivity-application-context/) 使用简单的应用程序示例,当您点击 iPhone 上的 UI 时,这些示例会向手表发送数据。
我的应用只是列出了来自我的 iPhone 应用的数据,所以我不需要立即发送数据,我只是想在应用加载或进入后台时发送它...为此我已经在 didFinishLaunching
和 didEnterBackground
中创建了 updateApplicationContext
...但是我的手表界面控制器中的数据源委托非常容易被触发...特别是 glance 只加载到模拟器,从不在设备上。有没有更好的时间和地点来推送信息?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
WatchSessionManager.sharedManager.startSession()
do {
try WatchSessionManager.sharedManager.updateApplicationContext(["peopleDict" : peopleDict])
} catch {
print(error)
}
return true
}
func applicationDidEnterBackground(application: UIApplication) {
do {
try WatchSessionManager.sharedManager.updateApplicationContext(["peopleDict" : peopleDict])
} catch {
print(error)
}
}
下面是我的 WatchSessionManager
我曾经在 extensionDelegate
的 appliciationDidFinishLaunching
activiateSession
import WatchConnectivity
protocol DataSourceChangedDelegate {
func dataSourceDidUpdate(dataSource: DataSource)
}
class WatchSessionManager: NSObject, WCSessionDelegate {
static let sharedManager = WatchSessionManager()
private override init() {
super.init()
}
private var dataSourceChangedDelegates = [DataSourceChangedDelegate]()
private let session: WCSession = WCSession.defaultSession()
func startSession() {
session.delegate = self
session.activateSession()
}
func addDataSourceChangedDelegate<T where T: DataSourceChangedDelegate, T: Equatable>(delegate: T) {
dataSourceChangedDelegates.append(delegate)
}
func removeDataSourceChangedDelegate<T where T: DataSourceChangedDelegate, T: Equatable>(delegate: T) {
for (index, indexDelegate) in dataSourceChangedDelegates.enumerate() {
if let indexDelegate = indexDelegate as? T where indexDelegate == delegate {
dataSourceChangedDelegates.removeAtIndex(index)
break
}
}
}
}
// MARK: Application Context
// use when your app needs only the latest information
// if the data was not sent, it will be replaced
extension WatchSessionManager {
// Receiver
func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) {
dispatch_async(dispatch_get_main_queue()) { [weak self] in
self?.dataSourceChangedDelegates.forEach { [=11=].dataSourceDidUpdate(DataSource(data: applicationContext))}
}
}
}
由于 updateApplicationContext
只存储最新的应用程序上下文,您可以随时更新它。手表只会获取最新的数据。没有包含旧上下文的队列。
在手表端,激活会话和设置 WCSessionDelegate 的最安全位置是在 ExtensionDelegate
init
方法中:
class ExtensionDelegate: NSObject, WKExtensionDelegate {
override init() {
super.init()
WatchSessionManager.sharedManager.startSession()
}
...
}
您的 Glance 没有更新,因为当显示 Glance 时,applicationDidFinishLaunching
没有被调用(因为当只启动 Glance 时手表应用程序没有启动)