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

标签 ios swift watchkit apple-watch watchconnectivity

一些详细介绍 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/

相关文章:

user-interface - 如何隐藏或删除 Apple Watch 状态栏中的时间?

ios - xcode ios - 表格 View 部分标题中的图像

ios - 计算器中的历史(Swift)

ios - #selector 指令在 Xcode 7.3 中的使用

ios - 关闭辅助 View Controller 后,如何继续从 Apple Watch 接收消息/数据

ios - 如何使用 iOS WatchKit 将 WKInterfaceButton 的标题文本居中

ios - 使用 Swift + SpriteKit 将节点移动到手指

ios - 在 iOS 的 UIDatePicker 中隐藏日期

ios - 根据传入的值显示大量文本

swift - AVSpeechSynthesiser 窃听 ViewDidLoad 功能