Problema de localização do WatchKit Core
Estou tentando ler as coordenadas de localização atual no WatchKitExtension ExtensionDelegate. Isso, porém, não retorna nenhum valor.
O mesmo código usado no WatchKitExtension InterfaceController retorna o local. (tentei isso por desespero, pois não consegui encontrar um erro no código)
Eu precisaria executar esse código no ExtensionDelegate, pois gostaria de passar o local recuperado para uma complicação do ClockKit.
Aqui o código em ExtensionDelegate: (após self.locationManager.requestLocation (), as funções delegadas didUpdateLocation / didFailWithError não são chamadas)
import WatchKit
import CoreLocation
class ExtensionDelegate: NSObject, WKExtensionDelegate, CLLocationManagerDelegate {
private let locationManager = CLLocationManager()
override init() {
print("ExtensionDelegate: \(NSDate()) - init")
super.init()
self.getLocation()
}
func getLocation(){
print("ExtensionDelegate: \(NSDate()) - getLocation")
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
locationManager.requestLocation()
}
...
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("ExtensionDelegate: \(NSDate()) - locationManager didUpdateLocations")
guard let mostRecentLocation = locations.last else { return }
let place = mostRecentLocation.coordinate
print("\(place)")
manager.stopUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("ExtensionDelegate: \(NSDate()) - locationManager didFailWithError")
print("CL failed: \(error)")
}
}
Aqui o mesmo código no InterfaceController e ele funciona perfeitamente (didUpdateLocation é chamado):
import WatchKit
import Foundation
import CoreLocation
class InterfaceController: WKInterfaceController, CLLocationManagerDelegate {
private let locationManager = CLLocationManager()
override func awakeWithContext(context: AnyObject?) {
print("InterfaceController: \(NSDate()) - awakeWithContext")
super.awakeWithContext(context)
self.getLocation()
}
func getLocation(){
print("InterfaceController: \(NSDate()) - getLocation")
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
locationManager.requestLocation()
}
...
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("InterfaceController: \(NSDate()) - locationManager didUpdateLocations")
guard let mostRecentLocation = locations.last else { return }
let place = mostRecentLocation.coordinate
print("\(place)")
manager.stopUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("InterfaceController: \(NSDate()) - locationManager didFailWithError")
print("CL failed: \(error)")
}
}