1

I've tried to get WatchConnectivity to work, but I can't send (location) messages from my iPhone to my Apple Watch. In AppDelegate I set up the session:

var session: WCSession!    

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    if WCSession.isSupported() {
        session = WCSession.defaultSession()
        session.delegate = self
        session.activateSession()
    }
    return true
}

In my ViewController:

let manager = CLLocationManager!

override func viewDidLoad() {
    super.viewDidLoad()
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.startUpdatingLocation()
    self.manager.delegate = self
    // CLLocationManager status handling code
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { // I know this is called
    if let locationData = manager.location {
        let location = ["location" : locationData]
        (UIApplication.sharedApplication().delegate as! AppDelegate).session.sendMessage(locationData, replyHandler: nil, errorHandler: nil)
    }
}

Here the relevant Interface Controller code:

var session: WCSession!

override func willActivate() {
    // This method is called when watch view controller is about to be visible to user
    super.willActivate()
    if WCSession.isSupported() {
        session = WCSession.defaultSession()
        session.delegate = self
        session.activateSession()
    }
}


func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) { // This method is never called
    let location = message["location"] as? CLLocation
    print(location)
}

However, my InterfaceController does not receive the message, nothing is printed, and the breakpoint I put is not triggered.

Can anyone point me in the right direction? Thanks.

saagarjha
  • 2,221
  • 1
  • 20
  • 37

1 Answers1

0

The contents of the dictionary passed to sendMessage can only contain property list types (basics such as numbers, strings, data, etc). I bet if you added an error handler you'd get an error indicating the payload cannot be sent because it contains invalid (non plist) types

Also, when you call sendMessage if you pass in a nil replyHandler a different delegate method is called on the receiving side than if there is a replyHandler. Try implementing the other delegate didReceiveMessage method:

func session(session: WCSession, didReceiveMessage message: [String : AnyObject])
ccjensen
  • 4,578
  • 2
  • 23
  • 25