I want to get the location of the user when press a button, I found a way to do that with CLLocationManager, that code work perfectly until the device the device doesn't have network connection, the function func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) catch an error
((NSError?) error = domain: "kCLErrorDomain" - code: 2 { _userInfo = nil })
How can I get the device location without internet connection? here is the code I use
import CoreLocation
class SesionViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: { (placemarks, error) -> Void in
if (error != nil) {
print("Error:" + error!.localizedDescription)
return
}
if placemarks!.count > 0 {
let pm = placemarks![0] as CLPlacemark
print(pm!.coordinate.latitude)
}else {
print("Error with data")
}
})
}
func startButtonPress(sender:UIButton!){
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.locationManager.stopUpdatingLocation()
}
}
Edit
I already found the solution, I just don't execute CLGeocoder().reverseGeocodeLocation... just catch the location with the locations var like this
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print(manager.location!.coordinate.latitude)
}