从 iPhone 发送和接收消息以从多个控制器观看

Send and receive messages from iPhone to Watch from multiple Controllers

处理从多个控制器发送和接收的通信的正确方法是什么?

我想做的是从 iOS 的两个不同 viewController 向 WatchOS 中的两个不同 interfaceController 发送消息(分开,而不是同时)。

这是我所拥有的,它只适用于 ViewController2 和 InterfaceController2 之间的通信,当消息从 ViewController1 发送到 InterfaceController1 时它会崩溃,因为它似乎是针对 session 一直来自 InterfaceController2 的方法。

ViewController 1:

class ViewController1: UIViewController,WCSessionDelegate{
    var session: WCSession!
    override func viewDidLoad() {
      super.viewDidLoad()
        if WCSession.isSupported() {
          session = WCSession.default()
          session.delegate = self
          session.activate()
        }
     }

      func sendDataToWatch(){
        let sendPrice:[String: Double] = ["price": 3.99]
        session.sendMessage(sendPrice, replyHandler: { replyMessage in
            // Some reply here, this could be nil
        }, errorHandler: {error in
            // Catch any errors here, this could be nil
            print("Error: \(error.localizedDescription)")
        })
     }
}

InterfaceController 1: 接收消息形式 ViewController 1

class InterfaceController1: WKInterfaceController, WCSessionDelegate{
   var session: WCSession!

    override func awake(withContext context: Any?) {
        super.awake(withContext: context)

        if (WCSession.isSupported()) {
            session = WCSession.default()
            session.delegate = self
            session.activate()
        }
    }

   func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) {

    /// Capture data from ViewContorller 2
    let priceFromPhone = message["price"] as? String

    // do something with priceFromPhone
   } 
}

// ===========================================

ViewController 2:

class ViewController2: UIViewController,WCSessionDelegate{
    var session: WCSession!
    override func viewDidLoad() {
      super.viewDidLoad()
        if WCSession.isSupported() {
          session = WCSession.default()
          session.delegate = self
          session.activate()
        }
     }

   func sendDataToWatch(){
    let sendEngine:[String: Double] = ["engine": 2.5]
    session.sendMessage(sendEngine, replyHandler: { replyMessage in
        // Some reply here, this could be nil
    }, errorHandler: {error in
        // Catch any errors here, this could be nil
        print("Error: \(error.localizedDescription)")
    })
  }
}

InterfaceController 2:从 ViewController 2

接收消息
class InterfaceController2: WKInterfaceController, WCSessionDelegate{
   var session: WCSession!

    override func awake(withContext context: Any?) {
        super.awake(withContext: context)

        if (WCSession.isSupported()) {
            session = WCSession.default()
            session.delegate = self
            session.activate()
        }
    }

   func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) {

      /// Capture data from ViewContorller 2
      let engineFromPhone = message["engine"] as? String

      // do something with engineFromPhone
    }    
 }

谢谢

根据我所阅读的内容,看来我需要将通信范围缩小到只有一个 ViewController 和一个 InterfaceController,然后通过 NSNotification 或 [=11= 共享更改].

我建议从处理您的 UI 的控制器中删除所有数据管理。这是一个糟糕的设计,以后像这样混合图层可能会让你头疼。

您应该有一个数据管理器,它是 WCSession 委托并负责保存信息,然后通知相关方(视图控制器等)支持数据已更新。