Search code examples
iosswiftcllocation

not able to reliably update location ios 8


I've recently gotten into mobile programming. The code below is finicky; sometimes it works and sometimes it doesn't. I've tried suggestions I've found on here like restarting location services on the virtual iphone. It is not a info.plist issue: I have NSLocationWhenInUseUsageDescription set, and when I'm debugging I can see it has WhenInUse authorization. When I debug I get to the println statement in didFailWithError and it reads "The operation couldn’t be completed. (kCLErrorDomain error 0.)". I've put the code behind a button to try it out. Is the reliability of this a known issue? If I load the app onto a device will it go away? Any thoughts appreciated. Thanks.

EDIT

This is in ViewController.swift in a single view template application.

let manager = CLLocationManager()
    @IBAction func getMyLocation (sender : AnyObject){
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        let status = CLLocationManager.authorizationStatus()
        if status == CLAuthorizationStatus.NotDetermined
        {
            manager.requestWhenInUseAuthorization()
        }
        manager.startUpdatingLocation()
    }
    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
       println("locations = \(locations)")
        manager.stopUpdatingLocation()

    }

    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!){
        println(error)
        manager.stopUpdatingLocation()
    }

Solution

  • I download the the app developer kit from Apple, and when I run the app with this script on my iphone the location is reliably retrieved. Looks like it's just an iOS simulator issue.