0

I have this code in my ViewController:

@IBAction func testButton(sender: UIButton) {
        let locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        print("\(CLLocationManager.locationServicesEnabled())")
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
        print("Started updating location")
    }

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let userLocation: CLLocation = locations[0]
        let long = userLocation.coordinate.longitude
        let lat = userLocation.coordinate.latitude
        print("\(long), \(lat)")
    }

    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
        print("Location update failed: \(error)")
    }

NSLocationWhenInUseUsageDescription in my plist. It should just work according to all information I could find, but it just outputs

true
Started updating location

Is edit 2 of this question true? If so, what would be the best way to move this out of the viewController?

Community
  • 1
  • 1
Fr4nc3sc0NL
  • 555
  • 1
  • 6
  • 22
  • Try with declaring `locationManager` as a property of your class. (`var locationManager:CLLocationManager!`)? – Larme Feb 17 '16 at 13:52
  • nope :/ also tried `static var` but not working either... (I used `var locationManager = CLLocationManager()` btw because otherwise I get a "no initialisers" error – Fr4nc3sc0NL Feb 17 '16 at 13:57
  • Did you do like in this answer:http://stackoverflow.com/a/24696739/1801544 ? Outside a `func`? – Larme Feb 17 '16 at 14:00
  • I forgot the "!".., so tried it with "!" but doesn't change anything – Fr4nc3sc0NL Feb 18 '16 at 19:34

1 Answers1

0

Before startUpdatingLocation(), have to ask for permission using requestWhenInUseAuthorization() first.

So your codes in ViewController will be similar like the following or you can download a sample project here.

class ViewController: UIViewController, CLLocationManagerDelegate {
    let locationManager: CLLocationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.testButton()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    func testButton() {
        if CLLocationManager.authorizationStatus() != .AuthorizedWhenInUse {
                self.locationManager.requestWhenInUseAuthorization()
                return
        }

        self.locationManager.startUpdatingLocation()
    }

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let userLocation: CLLocation = locations[0]
        let long = userLocation.coordinate.longitude
        let lat = userLocation.coordinate.latitude
        print("\(long), \(lat)")
    }

    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
        print("Location update failed: \(error)")
    }

    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        if status == .AuthorizedWhenInUse {
                self.testButton()
        }
    }
}
Allen
  • 1,714
  • 17
  • 19
  • locationManager(_:didChangeAuthorizationStatus: ) is never called – Fr4nc3sc0NL Feb 18 '16 at 19:31
  • Did you see a request permission dialog? https://db.tt/g0UY1vL7 If not, you probably would like to check your setting ``NSLocationWhenInUseUsageDescription`` in Info.plist. Also did you try the sample project I provided? – Allen Feb 19 '16 at 02:32
  • well, I solved it.... Turned out I added `NSLocationWhenInUseUsageDescription` to the tests plist instead of the main files plist.... but thanks for your help anyway :) your implementation is also much cleaner than mine ;) – Fr4nc3sc0NL Feb 19 '16 at 12:15