我试图让WatchConnectivity正常工作,但我无法将iPhone上的(位置)消息发送到Apple Watch。 在AppDelegate中,我设置了会话:
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
}
在我的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)
}
}
这里是相关的接口控制器代码:
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)
}
但是,我的InterfaceController没有收到消息,没有打印任何内容,并且我没有触发断点。
有人能指出我正确的方向吗?感谢。