gpt4 book ai didi

ios - 在哪里最好使用 Watch Connectivity 调用 updateApplicationContext?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:13:36 25 4
gpt4 key购买 nike

一些详细介绍 Watch Connectivity 的好博文(http://www.kristinathai.com/watchos-2-tutorial-using-application-context-to-transfer-data-watch-connectivity-2/http://natashatherobot.com/watchconnectivity-application-context/)使用简单的应用程序示例,当您点击 iPhone 上的 UI 时,这些示例会将数据发送到 watch 。

我的应用程序只是列出了来 self 的 iPhone 应用程序的数据,所以我不需要立即发送数据,我只是想在应用程序加载或进入后台时发送它...为此我制作了 updateApplicationContextdidFinishLaunchingdidEnterBackground 中……但是我的 watch 界面 Controller 中的数据源委托(delegate)非常容易被触发……特别是 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 我以前在我的activiateSessionextensionDelegate中调用appliciationDidFinishLaunching

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 { $0.dataSourceDidUpdate(DataSource(data: applicationContext))}
}

}
}

最佳答案

因为 updateApplicationContext 只存储最新的应用程序上下文,您可以随时更新它。 watch 只会获取最新的数据。没有包含旧上下文的队列。

在 watch 端,激活 session 和设置 WCSessionDelegate 的最安全位置是在 ExtensionDelegate init 方法中:

class ExtensionDelegate: NSObject, WKExtensionDelegate {

override init() {
super.init()
WatchSessionManager.sharedManager.startSession()
}
...
}

您的 Glance 没有更新,因为当显示 Glance 时,applicationDidFinishLaunching 没有被调用(因为只有启动 Glance 时 watch 应用程序没有启动)

关于ios - 在哪里最好使用 Watch Connectivity 调用 updateApplicationContext?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33267431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com