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

时间:2016-04-02 00:12:53

标签: ios watch-os watchconnectivity wcsession ios9.3

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

我知道有些新功能必须主要在iOS应用程序端实现,而且还根据Developer Library在watch扩展中实现:

session:activationDidCompleteWithState:error:

sessionDidBecomeInactive:

sessionDidDeactivate:

我不确定该怎么做以及这些功能应运行的代码。

1 个答案:

答案 0 :(得分:6)

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

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

从手表切换:

  

如果启用了自动切换,并且用户从一个Apple Watch切换到另一个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 sample code演示正确使用WatchConnectivity框架,以支持使用多个Apple手表进行快速手表切换。它使用updateApplicationContext将配对手表中的指示符和颜色传递给手机。

其他说明:

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

有关详细信息,请参阅How can watchOS 2.2 app determine if its paired iPhone has switched to another Apple Watch?

<强>现金:

提供的代码基于QuickSwitch,以及{支持与多个Apple手表的通信下的WCSessionDelegate Protocol ReferenceWCSession Class Reference中的详细信息。