【问题标题】:Where best to call updateApplicationContext using Watch Connectivity?在哪里最好使用 Watch Connectivity 调用 updateApplicationContext?
【发布时间】:2015-10-21 19:15:03
【问题描述】:

一些详细介绍 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 时将数据发送到手表。

我的应用程序只是列出了我的 iPhone 应用程序中的数据,所以我不需要立即发送数据,我只是想在应用程序加载或进入后台时发送它......为此我做了@ didFinishLaunchingdidEnterBackground 中的 987654323@ ......但是我的手表界面控制器中的 dataSource 委托非常容易被触发......特别是一瞥只加载在模拟器上,从不加载到设备上。有更好的时间和地点推送信息吗?

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 我曾经在我的extensionDelegateappliciationDidFinishLaunching 中调用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 { $0.dataSourceDidUpdate(DataSource(data: applicationContext))}
        }

    }
}

【问题讨论】:

    标签: ios swift watchkit apple-watch watchconnectivity


    【解决方案1】:

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

    在手表端激活会话和设置 WCSessionDelegate 的最安全位置是在 ExtensionDelegate init 方法中:

    class ExtensionDelegate: NSObject, WKExtensionDelegate {
    
        override init() {
            super.init()
            WatchSessionManager.sharedManager.startSession()
        }
        ...
    }
    

    您的 Glance 不会更新,因为当 Glance 显示时,applicationDidFinishLaunching 没有被调用(因为仅启动 Glance 时手表应用程序没有启动)

    【讨论】:

    • 谢谢,也许我对API有误解...我以为updateApplicationContext会将数据发送到手表,当手表上激活会话时,dataSourceDelegate会观察到更新上下文并使其可供使用?换句话说,我怎样才能让更新的信息一目了然?我在 ExtensionDelegate 中做 activateSession。
    • 不,您不会误解 API 的工作原理。您在 ExtensionDelegate 的哪个位置激活会话?在applicationDidFinishLaunching 中,还是像我在回答中建议的那样覆盖init
    • 我将 WatchSessionManager 类添加到我的问题中
    • 我更新了我的答案,以便它从 ExtensionDelegate 的 init 方法调用您的 WatchSessionManager。这还是不行吗?
    • 所以将 startSession 从 applicationDidFinishLaunching 中取出并仅放入 init 中?
    猜你喜欢
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2016-10-04
    • 1970-01-01
    • 1970-01-01
    • 2016-04-28
    • 1970-01-01
    • 2015-09-19
    相关资源
    最近更新 更多