观看扩展配置

WatchKit 应用程序

  1. 导入 WatchConnectivity 并符合 WCSessionDelegate。
  2. 通过 WCSession.default() 使用静态会话委托。
  3. 使用以下方法将数据发送到 iPhone 应用:
 `WCSession.default()`.sendMessage(message, replyHandler:_ errorHandler:_)
  1. 消息对象应该是[String:Any]类型的字典
  2. 如果你正在寻找从 Watch 应用程序返回的数据,请在 replyHandler 中定义的闭包中提供逻辑; 否则,传递无。
  3. 要响应从 iPhone 应用程序发送的消息,你将使用 WCSessionDelegate 回调方法
func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Swift.Void){...}
  1. Watch 应用程序中不需要这些方法来正确符合 WCSessionDelegate:
 func sessionDidBecomeInactive(_ session: WCSession)
 func sessionDidDeactivate(_ session: WCSession)
 func sessionWatchStateDidChange(_ session: WCSession)

最后,通常最佳做法是将任何常用图像,图像和资源存储在 Watch 的 xcassets 文件夹中,这些图像和资源将不断用于 Watch 应用程序。好的,现在你感到困惑,让我们了解详细信息!

这可能是你的一个惊喜,但你需要再次导入 WatchConnectivity。

import WatchConnectivity

接下来,你需要验证会话是否可行。

// MARK: - View Life Cycle Callbacks
override func awake(withContext context: Any?) {
    super.awake(withContext: context)

    // Configure interface objects here.
    if WCSession.isSupported() {
        WCSession.default().delegate = self
        WCSession.default().activate()
        print("InterfaceController: Session Activated")

        // Request Data from iPhone App
        let requestMessage = ["message":"get-data"]

        WCSession.default().sendMessage(requestMessage, replyHandler: { (replyMessage) in
            print("Got a reply from the phone: \(replyMessage)")

            // handle reply message here

        }, errorHandler: { (error) in
            print("Got an error sending to the phone: \(error)")
        })
    }
    else {
        print("
ViewController: connectionManager is nil
")
    }
}

但除非你实现 WCSessionDelegate 所需的方法,否则这些都不会起作用。

extension InterfaceController : WCSessionDelegate {

    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
        print("1. InterfaceController: ", "activationDidCompleteWith activationState") // first
    }

    /** ------------------------- Interactive Messaging ------------------------- */

    func sessionReachabilityDidChange(_ session: WCSession) {
        print("2. InterfaceController: ", "sessionReachabilityDidChange") // second
    }

    func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
        print("3. InterfaceController: ", "didReceiveMessage")
    }

    func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Swift.Void) { // third
        print("4. InterfaceController: ", "didReceiveMessage")
        //print("Message Contents: ", message["message"]!)
    }

    func session(_ session: WCSession, didReceiveMessageData messageData: Data) {
        print("5. InterfaceController: ", "didReceiveMessageData")
    }

    func session(_ session: WCSession, didReceiveMessageData messageData: Data, replyHandler: @escaping (Data) -> Swift.Void) {
        print("6. InterfaceController: ", "didReceiveMessageData")
    }

    /** -------------------------- Background Transfers ------------------------- */

    func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
        print("7. InterfaceController: ", "didReceiveApplicationContext")
    }

    func session(_ session: WCSession, didFinish userInfoTransfer: WCSessionUserInfoTransfer, error: Error?) {
        print("8. InterfaceController: ", "didFinish userInfoTransfer")
    }

    func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any] = [:]) {
        print("9. InterfaceController: ", "didReceiveUserInfo")
    }

    func session(_ session: WCSession, didFinish fileTransfer: WCSessionFileTransfer, error: Error?) {
        print("10. InterfaceController: ", "didFinish fileTransfer")
    }

    func session(_ session: WCSession, didReceive file: WCSessionFile) {
        print("11. InterfaceController: ", "didReceive file")
    }    
}