mac*_*moo 2 ios watchos watchconnectivity wcsession ios9.3
无法确定如何为watchOS 2.2更新我的手表和iOS应用程序以支持多个手表.
我知道有些新功能必须主要在iOS应用程序端实现,而且还根据Developer Library在watch扩展上实现:
session:activationDidCompleteWithState:error:
sessionDidBecomeInactive:
sessionDidDeactivate:
Run Code Online (Sandbox Code Playgroud)
我不确定该怎么做以及这些函数应该运行什么代码.
小智 6
运行iOS 9.3或更高版本的iPhone可能与运行watchOS 2.2或更高版本的多个Apple Watch配对.
WCSessionDelegateiOS上需要所有这三种方法来支持快速切换手表所需的异步会话激活.
从手表切换:
启用自动切换后,用户从一个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")
}
Run Code Online (Sandbox Code Playgroud)
一旦传递了该数据,会话就会进入停用状态.此时,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()
}
Run Code Online (Sandbox Code Playgroud)
切换到手表:
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)")
}
Run Code Online (Sandbox Code Playgroud)
Apple提供了QuickSwitch示例代码,以演示如何正确使用WatchConnectivity框架,以支持使用多个Apple手表进行快速手表切换.它用于updateApplicationContext将指定器和颜色从配对的手表传递到手机.
其他说明:
未连接的监视应用程序可以继续使用所有传输方法,包括交互式消息传递(尽管传出的数据确实由系统排队,并且在用户切换回该监视之前不会传输).
有关详细信息,请参阅watchOS 2.2应用程序如何确定其配对的iPhone是否已切换到另一个Apple Watch?
积分:
提供的代码基于QuickSwitch,以及WCSessionDelegate Protocol Reference和WCSession Class Reference中的详细信息,位于支持与多个Apple Watch的通信下.
| 归档时间: |
|
| 查看次数: |
1185 次 |
| 最近记录: |