Error: "Message was received too long" - WCSession Watch OS2

Therefore, I use Watch Connectivity to request an array from iPhone to Watch.

The idea was sendMessage with a clock, and the iPhone would respond with an array in the didReceiveMessage method.

However, the iPhone does not seem to respond, I thought the iPhone would open the application when I send a message from Watch. I even tried to open the application when I sendMessage , but still no luck. When I wait long enough, I get the following error message:

Domain error = code WCErrorDomain = 7012 "The response message took too long." UserInfo = {NSLocalizedDescription = The response message took too long. NSLocalizedFailureReason = response timeout.}

Does anyone know where I could be wrong?

Apple watch

 import WatchKit import Foundation import CoreData import WatchConnectivity class BookmarkedInterfaceController: WKInterfaceController, WCSessionDelegate { var session : WCSession! var objects: [AnyObject]! @IBOutlet var table: WKInterfaceTable! override func willActivate() { super.willActivate() //Check if session is supported and Activate if (WCSession.isSupported()) { session = WCSession.defaultSession() session.delegate = self session.activateSession() } sendMessageToIphone() } func sendMessageToIphone() { if WCSession.defaultSession().reachable { print("WCSession is reachabe") let messageDict = ["Request": "iPhone Can You Give Me The Array"] WCSession.defaultSession().sendMessage(messageDict, replyHandler: { (replyDict) -> Void in print(replyDict) }, errorHandler: { (error) -> Void in print(error) }) } } func session(session: WCSession, didReceiveMessage message: [String : AnyObject]) { //recieving message from iphone print("recieved message from iphone \(message)") objects.append(message["Array"]!) print("Objects array = \(objects)") } 

Console outputs

WCSession is an achievement nil Array

iPhone App Delegate

 import UIKit import CoreData import WatchConnectivity @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate, WCSessionDelegate { var window: UIWindow? var session : WCSession! func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { //Check if session is supported and Activate if (WCSession.isSupported()) { session = WCSession.defaultSession() session.delegate = self session.activateSession() } } func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) { print("did recieve message from Watch") let applicationData = ["Array":["One", "Two", "Three"]] replyHandler(applicationData) } 

None of the iPhone is running. Even when I manually open the application.

+6
source share
2 answers

If you want a response to the message to be sent to send the requested data, you must change your code to the following:

Clock

 func sendMessageToIphone() { if WCSession.defaultSession().reachable { print("WCSession is reachabe") let messageDict = ["Request": "iPhone Can You Give Me The Array"] WCSession.defaultSession().sendMessage(messageDict, replyHandler: { (replyDict) -> Void in print("Array \(replyDict["array"])") }, errorHandler: { (error) -> Void in print(error) }) } } 

Telephone

 func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) { print("did recieve message from Watch") let applicationData = ["Array":["One", "Two", "Three"]] //If identifier from recievedMessage is for Objects replyHandler(applicationData) } 

And separately, the reason why sendMessage wasn’t received from the phone for hours is because you implemented the wrong delegation method to use the sendMessage call that you are using.

If you call sendMessage with nil replyHandler, then this delegate method will be called on the receiving side: func session(session: WCSession, didReceiveMessage message: [String : AnyObject])

If you call sendMessage with a non-nil replyHandler, then this delegate method will be called on the receiving side: func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void)

+3
source

You must activate the session before sending the message. You must also set a delegate before activating the session, because you may lose some pending messages.

iphone side:

 import UIKit import WatchConnectivity @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate, WCSessionDelegate { var window: UIWindow? var session : WCSession! func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) { print("did recieve message from Watch") let applicationData = ["Array":["One", "Two", "Three"]] //If identifier from recievedMessage is for Objects session.sendMessage(applicationData, replyHandler: { reply in print("Got reply: \(reply)") }, errorHandler: { error in print("error: \(error)") }) } func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { session = WCSession.defaultSession() session.delegate = self session.activateSession() return true } } 

Extending iWatch InterfaceController You must activate the session in the method of activating the controller interface.

 import WatchKit import Foundation import WatchConnectivity class InterfaceController: WKInterfaceController, WCSessionDelegate { var session : WCSession! var objects: [AnyObject]! override func awakeWithContext(context: AnyObject?) { super.awakeWithContext(context) // Configure interface objects here. } override func willActivate() { // This method is called when watch view controller is about to be visible to user super.willActivate() session = WCSession.defaultSession() session.delegate = self session.activateSession() objects = [] sendMessageToIphone() } override func didDeactivate() { // This method is called when watch view controller is no longer visible super.didDeactivate() } func sendMessageToIphone() { if WCSession.defaultSession().reachable{ print("WCSession is reachabe") let messageDict = ["Request": "iPhone Can You Give Me The Array"] WCSession.defaultSession().sendMessage(messageDict, replyHandler: { (replyDict) -> Void in print(replyDict) }, errorHandler: { (error) -> Void in print(error) }) } } func session(session: WCSession, didReceiveMessage message: [String : AnyObject]) { //recieving message from iphone print("recieved message from iphone \(message)") objects.append(message["Array"]!) print("Objects array = \(objects)") } } 

Note. First run the iphone app. Then run the extension and keep the iphone app in the foreground.

0
source

All Articles