I am trying to learn watchkit connectivity using a simple app and have spent two days vainly researching this error:
{app name}_WatchKit_Extension.InterfaceController does not implement delegate method.
I have slavishly copied code from the "SimpleWatchConnectivity" https://developer.apple.com/library/content/samplecode/SimpleWatchConnectivity/Listings/SimpleWatchConnectivity_WatchKit_Extension_InterfaceController_swift.html demo code. and have scoured the forums without success.
My code builds and runs OK but I get the following error on the watch
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
when I execute:
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)} )
}
}
from the iOS ViewController.
My interface controller is as follows:
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")
}
}
(Sorry about the formatting, I tried for 10 mins to get it right, but sadly failed)
I have added a bunch of print's to ensure all cases are covered.
the full error is:
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
Any assistance will be greatly appreciated
[EDIT] When I monitor the Phone app with the debugger, I see the send message fails with:
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 dropping as pairingIDs no longer match. pairingID (null), client pairingID: (null)
WatchConnectivity session has not been activated.
The error I receive from the watchKitExtension quoted at the top of the post is generated when I attempt to send a second message. There is no error in watchKitExtension from the first message.
Below Solution works well to share data between iPhone and iWatch via 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)")
}
}
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)
}
OutPut will be as below,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With