2

无法弄清楚如何为 watchOS 2.2 更新我的手表和 iOS 应用程序以支持多个手表。

我知道有一些新功能必须主要在 iOS 应用程序端实现,但根据开发人员库,还必须在手表扩展上实现:

session:activationDidCompleteWithState:error:

sessionDidBecomeInactive:

sessionDidDeactivate:

我不确定该怎么做以及这些函数应该运行什么代码。

4

1 回答 1

7

运行 iOS 9.3 或更高版本的 iPhone 可以与多个运行 watchOS 2.2 或更高版本的 Apple Watch 配对。

iOS 上需要所有这三种WCSessionDelegate方法来支持快速手表切换所需的异步会话激活。

从手表切换:

当启用自动切换并且用户从一个 Apple Watch 切换到另一个时,iOS 应用程序会在切换期间进入非活动停用状态。

移动到非活动状态使会话有少量时间来传递已经接收到的任何数据。

// MARK: WCSessionDelegate - Asynchronous Activation
// The next 3 methods are required in order to support asynchronous session activation; required for quick watch switching.

func sessionDidBecomeInactive(session: WCSession) { // iOS only
    /*
        The session calls this method when it detects that the user has
        switched to a different Apple Watch. While in the inactive state,
        the session delivers any pending data to your delegate object and
        prevents you from initiating any new data transfers. After the last
        transfer finishes, the session moves to the deactivated state.

        Use this method to update any private data structures that might be
        affected by the impending change to the active Apple Watch. For example,
        you might clean up data structures and close files related to
        outgoing content.
     */

    print("session did become inactive")
}

一旦传递了该数据,会话就会进入停用状态。此时,iOS 应用必须再次调用 activateSession 方法才能连接到新激活的手表。

func sessionDidDeactivate(session: WCSession) { // iOS only
    print("session did deactivate")

    /*
        The session calls this method when there is no more pending data
        to deliver to your app and the previous session can be formally closed.

        iOS apps that process content delivered from their Watch Extension
        should finish processing that content, then call activateSession()
        to initiate a session with the new Apple Watch.
     */

    // Begin the activation process for the new Apple Watch
    WCSession.defaultSession().activateSession()
}

切换到手表:

iOS 和 watchOS 应用程序都将实现以下方法,以便在其 WCSession 激活后调用:

func session(session: WCSession, activationDidCompleteWithState activationState: WCSessionActivationState, error: NSError?) {
    if let error = error {
        print("session activation failed with error: \(error.localizedDescription)")
        return
    }

    /*
        Called when the activation of a session finishes. Your implementation
        should check the value of the activationState parameter to see if
        communication with the counterpart app is possible. When the state is
        WCSessionActivationStateActivated, you may communicate normally with
        the other app.
     */

    print("session activated with state: \(activationState.rawValue)")
}

示例代码:

Apple 提供了QuickSwitch 示例代码来演示 WatchConnectivity 框架的正确使用,以支持多个 Apple Watch 的快速手表切换。它用于updateApplicationContext将指示符和颜色从配对的手表传递到手机。

其他注意事项:

未连接的手表应用程序可以继续使用所有传输方法,包括交互式消息传递(尽管传出数据确实会被系统排队,并且在用户切换回该手表之前不会传输)。

有关更多详细信息,请参阅watchOS 2.2 应用程序如何确定其配对的 iPhone 是否已切换到另一台 Apple Watch?

学分:

提供的代码基于QuickSwitch,详细信息可在WCSessionDelegate 协议参考WCSession 类参考中的 Supporting Communication with Multiple Apple Watches 下找到。

于 2016-04-02T01:20:30.983 回答