WatchKit_Extension.InterfaceController没有实现委托方法

Sta*_*raX 4 swift apple-watch watchkit watchconnectivity

我正在尝试使用一个简单的应用程序学习watchkit连接,并花了两天徒劳地研究这个错误:

{app name} _WatchKit_Extension.InterfaceController不实现委托方法.

我已经盲目地复制了"SimpleWatchConnectivity" https://developer.apple.com/library/content/samplecode/SimpleWatchConnectivity/Listings/SimpleWatchConnectivity_WatchKit_Extension_InterfaceController_swift.html演示代码中的代码.并且在没有成功的情况下搜索了论坛.

我的代码构建并运行正常,但我在手表上收到以下错误

2018-01-06 10:39:20.522995 + 1100 messageDemo WatchKit Extension [338:743006] [WC] - [WCSession onqueue_handleDictionaryMessageRequest:withPairingID:] _ block_invoke委托messageDemo_WatchKit_Extension.InterfaceController没有实现委托方法2018-01-06 10:39: 20.524378 + 1100 messageDemo WatchKit扩展[338:743006] [WC] - [WCSession _onqueue_sendResponseError:identifier:dictionaryMessage:]标识符:A150D814-453C-44B7-B970-913697526D6A,带有WCErrorCodeDeliveryFailed

当我执行时:

2018-01-06 10:39:20.522995+1100 messageDemo WatchKit Extension[338:743006] [WC] -[WCSession onqueue_handleDictionaryMessageRequest:withPairingID:]_block_invoke delegate messageDemo_WatchKit_Extension.InterfaceController does not implement delegate method
2018-01-06 10:39:20.524378+1100 messageDemo WatchKit Extension[338:743006] [WC] -[WCSession _onqueue_sendResponseError:identifier:dictionaryMessage:] identifier: A150D814-453C-44B7-B970-913697526D6A with WCErrorCodeDeliveryFailed
Run Code Online (Sandbox Code Playgroud)

来自iOS ViewController.

我的界面控制器如下:

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

        if let messageText =  textField.text{  
             WCSession.default.sendMessage(["message": messageText]  
                ,replyHandler: {replyMessage in  
                    print(replyMessage)  
             }  

               , errorHandler: {error in  
                print( error.localizedDescription)}  )  
        }  
    }  
Run Code Online (Sandbox Code Playgroud)

import Foundation
import WatchConnectivity
类InterfaceController:WKInterfaceController
,WCSessionDelegate
{
func session(_ session:WCSession
,activationDidCompleteWith activationState:WCSessionActivationState
,error:Error?){
print("activationDidCompleteWith:(activationState)")/
}
func sessionReachabilityDidChange(_ session:WCSession) {
/
/
print("sessionReachabilityDidChange")
}

 import WatchKit  
import Foundation  
import WatchConnectivity  
class InterfaceController: WKInterfaceController  
    , WCSessionDelegate  
    {  
    func session(_ session: WCSession  
        , activationDidCompleteWith activationState: WCSessionActivationState  
        , error: Error?) {  
        print("activationDidCompleteWith:\(activationState )") /  
    }  
    func sessionReachabilityDidChange(_ session: WCSession) {  
      /  
      /  
        print("sessionReachabilityDidChange")  
    }  

    @IBOutlet var messageLabel: WKInterfaceLabel!  
    override func awake(withContext context: Any?) {  
        super.awake(withContext: context)  

        /  
    }  

    override func willActivate() {  
        /  
        super.willActivate()  
        if (WCSession.isSupported())  
        {  
            WCSession.default.delegate = self;  
            WCSession.default.activate()  

        }  

    }  

    override func didDeactivate() {  

        super.didDeactivate()  

    }  

    private func session(session: WCSession, didReceiveMessage message: [String : Any]  
        , replyHandler: @escaping ([String : Any]) -> Void) {  
        self.messageLabel.setText(message["message"]! as? String)  
    }  
       func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {  
        self.messageLabel.setText(message["message"]! as? String)  
    }  
    func session(_ session: WCSession, didReceiveMessageData messageData: Data) {  
        print("HERE01")  
    }  

    func session(_ session: WCSession, didReceiveMessageData messageData: Data, replyHandler: @escaping (Data) -> Void) {  
        self.session(session, didReceiveMessageData: messageData)  

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

         print("HERE03")  
    }  

    func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any] = [:]) {  
         print("HERE04")  

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

    func session(_ session: WCSession, didReceive file: WCSessionFile) {  
        print("HERE06")  
        }  

    func session(_ session: WCSession, didFinish fileTransfer: WCSessionFileTransfer, error: Error?) {  
        print("HERE07")  
    }  
}  
Run Code Online (Sandbox Code Playgroud)

}
(很抱歉的格式,我尝试了10分钟,得到它的权利,但可悲的失败)

我添加了一堆打印件以确保涵盖所有案例.

完整的错误是:

2018-01-06 10:39:20.522995 + 1100 messageDemo WatchKit Extension [338:743006] [WC] - [WCSession onqueue_handleDictionaryMessageRequest:withPairingID:] _ block_invoke委托messageDemo_WatchKit_Extension.InterfaceController没有实现委托方法2018-01-06 10:39: 20.524378 + 1100 messageDemo WatchKit扩展[338:743006] [WC] - [WCSession _onqueue_sendResponseError:identifier:dictionaryMessage:]标识符:A150D814-453C-44B7-B970-913697526D6A,带有WCErrorCodeDeliveryFailed

任何帮助将不胜感激

[编辑]当我使用调试器监视电话应用程序时,我看到发送消息失败:

2018-01-07 09:47:23.319221 + 1100 messageDemo [6628:1549809] [WC] - [WCSession _onqueue_notifyOfMessageError:messageID:withErrorHandler:](null)errorHandler:YES with WCErrorCodeSessionNotActivated 2018-01-07 09:47:23.321103+ 1100 messageDemo [6628:1549978] [WC] - [WCSession _onqueue_notifyOfMessageError:messageID:withErrorHandler:] _ block_invoke drop for pairingIDs不再匹配.pairingID(null),client pairingID:(null)尚未激活WatchConnectivity会话.

当我尝试发送第二条消息时,会生成从帖子顶部引用的watchKitExtension收到的错误.第一条消息中watchKitExtension没有错误.

iGa*_*hah 5

以下解决方案适用于在iPhone和iWatch之间共享数据WCSession,

ViewController.swift

import UIKit
import WatchConnectivity

class ViewController: UIViewController, WCSessionDelegate {

    @IBOutlet weak var textFieldMessage : UITextField!
    @IBOutlet weak var buttonSend : UIButton!
    var wcSession : WCSession!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        wcSession = WCSession.default
        wcSession.delegate = self
        wcSession.activate()
    }

    //MARK: - Button Actions

    @IBAction func clickSendMessage(_ sender : UIButton) {

        let message = ["message" : textFieldMessage.text!]
        do {
            try wcSession.updateApplicationContext(message)
        } catch {
            print("Something went wrong")
        }
    }

    // MARK: - WCSessionDelegate

    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
        NSLog("%@", "activationDidCompleteWith activationState:\(activationState) error:\(String(describing: error))")
    }

    func sessionDidBecomeInactive(_ session: WCSession) {
        print("%@", "sessionDidBecomeInactive: \(session)")
    }

    func sessionDidDeactivate(_ session: WCSession) {
        print("%@", "sessionDidDeactivate: \(session)")
    }

    func sessionWatchStateDidChange(_ session: WCSession) {
        print("%@", "sessionWatchStateDidChange: \(session)")
    }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

InterfaceController.swift(Watch Extension)

import WatchKit
import Foundation
import WatchConnectivity

class InterfaceController: WKInterfaceController, WCSessionDelegate {

    var session : WCSession?
    @IBOutlet weak var sessionLabel : WKInterfaceLabel!

    override func willActivate() {
        // This method is called when watch view controller is about to be visible to user
        super.willActivate()

        session = WCSession.default
        session?.delegate = self
        session?.activate()
    }

    // MARK: - WCSessionDelegate

    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
        NSLog("%@", "activationDidCompleteWith activationState:\(activationState) error:\(String(describing: error))")
    }

    func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
        NSLog("didReceiveApplicationContext : %@", applicationContext)
        sessionLabel.setText(applicationContext["message"] as? String)
    }
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

OutPut如下,

在此输入图像描述