首页 > 解决方案 > didReceiveMessage 接收并响应消息但未调用方法

问题描述

观看应用界面控制器:

@IBAction func scheduleMeeting(_ sender: WKInterfaceButton) {
        if (WCSession.isSupported()) {
            session.sendMessage([ "scheduleMeeting": ["scheduleMeeting"] ], replyHandler: { (responses) -> Void in
                print("response: \(responses)")
            }) { (err) -> Void in
                print("error: \(err)")
            }
        }
    }

iOS 应用主视图控制器:

class ParentViewController: UIViewController, WCSessionDelegate {
    var session: WCSession?

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

        //FYI i have tried this  -DispatchQueue.main.async { - but no difference
        print("test1")
        do {
            //since we are passing true for asynchronous we cannot use the result of the call, the warning below is n/a
            try 
Communicator.addScheduledMeeting(self.scheduledMeeting, asynchronous: true)
        } catch {
            print("Unexpected saving scheduled meeting error: \(error).")
        }

        // Send a reply
        replyHandler(["Message":"Hi from app"])

    }
//omitted rest of class

正如我们从控制台输出(下图)中看到的那样,应用程序接收到消息并发送回响应

回复:[“消息”:来自应用程序的您好]

我的问题是没有其他代码运行

旁注我不知道它是否相关,但我觉得它可能是,我应该提到它。
要运行我的手表应用程序,我选择“WatchKit App”并点击播放(它在我的控制台输出的这种模式下)。但是在此期间,我的应用程序仅出现在手表模拟器上。如果我尝试在 iPhone 模拟器上打开它,它会打开显示活动指示器,然后变黑然后返回主屏幕。如果我只选择“”并点击播放,应用程序运行良好。我可以手动启动手表应用程序,但对控制台没有任何影响,也没有任何断点。

标签: swiftwcsession

解决方案


推荐阅读