En el proceso de aprender a obtener la ubicación del usuario en una clase singleton, descargué este código https://github.com/irfanlone/CLLocationManager-Singleton-Swift. Después de forzarlo para que funcione en Xcode8.3.3 Swift 3, no puedo obtener ningún resultado en la consola de depuración. De hecho, ni siquiera puedo obtener este dlalog de autenticación de la ubicación para mostrar. Sospecho que los datos del singleton no se pasan al controlador de vista, pero no puedo solucionar la solución, ¿puedes ver lo que está mal? Gracias.
El código corregido para Swift 3 es: // El singleton: importación MapKit
protocolo LocationUpdateProtocol { func locationDidUpdateToLocation (_ ubicación: CLLocation) }
dejar kLocationDidChangeNotification = "LocationDidChangeNotification"
clase UserLocationManager: NSObject, CLLocationManagerDelegate {
static let SharedManager = UserLocationManager()
fileprivate var locationManager = CLLocationManager()
var currentLocation : CLLocation?
var delegate : LocationUpdateProtocol!
fileprivate override init() {
super.init()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.distanceFilter = kCLLocationAccuracyHundredMeters
locationManager.requestAlwaysAuthorization()
self.locationManager.startUpdatingLocation()
}
// MARK: - CLLocationManagerDelegate
func locationManager(manager: CLLocationManager,didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) {
currentLocation = newLocation
let userInfo : NSDictionary = ["location" : currentLocation!]
DispatchQueue.main.async() {() -> Void in
self.delegate.locationDidUpdateToLocation(self.currentLocation!)
NotificationCenter.default.post(name: Notification.Name(kLocationDidChangeNotification), object: self, userInfo: userInfo as [NSObject : AnyObject])
}
}
}
// El ViewController UIKit importación CoreLocation importación
ViewController clase: UIViewController, LocationUpdateProtocol {
var currentLocation : CLLocation!
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.locationUpdateNotification(_:)), name: NSNotification.Name(rawValue: kLocationDidChangeNotification), object: nil)
let LocationMgr = UserLocationManager.SharedManager
LocationMgr.delegate = self
}
// MARK: - Notifications
func locationUpdateNotification(_ notification: Notification) {
let userinfo = notification.userInfo
self.currentLocation = userinfo!["location"] as! CLLocation
print("Latitude : \(self.currentLocation.coordinate.latitude)")
print("Longitude : \(self.currentLocation.coordinate.longitude)")
}
// MARK: - LocationUpdateProtocol
func locationDidUpdateToLocation(_ location: CLLocation) {
currentLocation = location
print("Latitude : \(self.currentLocation.coordinate.latitude)")
print("Longitude : \(self.currentLocation.coordinate.longitude)")
}
}
'locationManager: didUpdateToLocation: fromLocation' es un método delegado. El administrador de la ubicación usa este método para informar a su objeto que la ubicación se actualizó con un nuevo valor; usted debe implementar este método para hacer algo con el valor. – quellish