WCErrorCodeDeliveryFailed: Payload could not be delivered
Asked Answered
H

2

20

I'm working on an app that share data between iPhone and Apple Watch, using WCSession method sendMessage:replyHandler:errorHandler:

After implementing that method I get the error like:

WCSession _onqueue_notifyOfMessageError:withErrorHandler: errorHandler: YES with WCErrorCodeDeliveryFailed.

Error = Payload could not be delivered.

import Foundation
import WatchKit
import WatchConnectivity

class ResultInterfaceController: WKInterfaceController, WCSessionDelegate {

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

    let applicationData = ["name": "ViratKohli"]
    self.sendToPhone(data: applicationData)
}

func sendToPhone(data: [String: Any]) {

    if WCSession.isSupported() {

        let session = WCSession.default
        session().delegate = self
        session().activate()

        if WCSession.default().isReachable {

            session().sendMessage(data, replyHandler: {(_ replyMessage: [String: Any]) -> Void in

                print("ReplyHandler called = \(replyMessage)")
                WKInterfaceDevice.current().play(WKHapticType.notification)
            }, 
            errorHandler: {(_ error: Error) -> Void in

                print("Error = \(error.localizedDescription)")
            })
         }
    }
}
....

Any help appreciated.

Haeckel answered 10/2, 2017 at 4:22 Comment(2)
Maybe this does help you?Metsky
@ReinhardMänner nope. That didn'tHaeckel
W
16
  1. Do you have session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) on ios side's WCSessionDelegate?
  2. Are you calling replyHandler() inside this method?

Pay attention that session(_ session: WCSession, didReceiveMessage message: [String : Any]) will be called only for messages sent without replyHandler.

Williamsen answered 22/3, 2017 at 12:52 Comment(3)
This is the correct answer. If a replyHandler is provided but the other app does not implement the variant with replyHandler, the request will fail.Andalusite
Thanks, struggled with watch > phone communication until I read this post/answer and tried the session:message:replyHandler: + call replyHandler() route!Keeleykeelhaul
Thank you! works greatJury
H
11

I was in the same trouble. If you are sending a message with a replyHandler , you'll have to use

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

}

instead of

func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {

}

for receiving messages.

Hatred answered 26/4, 2018 at 7:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.