如何支持 iOS 9.3 和 watchOS 2.2 的多手表

How to support multiple watches for iOS 9.3 and watchOS 2.2

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

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

session:activationDidCompleteWithState:error:

sessionDidBecomeInactive:

sessionDidDeactivate:

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

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

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

从手表切换:

When automatic switching is enabled, and the user switches from one Apple Watch to another, the iOS app moves to the inactive and deactivated states during a switch.

Moving to the inactive state gives the session a small amount of time to deliver any data that has already been received.

// 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")
}

As soon as that data is delivered, the session moves to the deactivated state. At that point, the iOS app must call the activateSession method again to connect to the newly active watch.

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 sample code 来演示 WatchConnectivity 框架的正确使用,以支持与多个 Apple Watch 的快速手表切换。它使用 updateApplicationContext 将配对手表的标识符和颜色传递给 phone。

其他说明:

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

有关详细信息,请参阅

学分:

提供的代码基于 QuickSwitch, and details found in the WCSessionDelegate Protocol Reference, and WCSession Class Reference,在支持与多个 Apple Watch 的通信下。