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.