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.