3
votes

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)
}
1
What device were you testing on? Not all iOS devices have GPS hardware so some devices requires an Internet connection.rmaddy
did you had those two string into your p.list, and also you should use the status method to actually know if you are fully granted accessLamour
yes, I already have those strings... the problem was I shouldn't have implemented the contact with appleKristian Sthefan Cortes Prieto
@rmaddy can you tell me which devices have GPS hardware and which are not?Himanth

1 Answers

1
votes

from Apple doc about ClGeocoder

The computer or device must have access to the network in order for the geocoder object to return detailed placemark information

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")
            }

        })

will always report an error without an internet access. Your location data are still available (if your device has GPS unit)