觀看擴充套件配置

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