0

Inside Location Services I allow my App to always use location. In viewDidLoad I created a timer which calls my method every 2 seconds:

println("---\(locationManager.location)")

In the beginning I get ---nil, but when I go to the settings and change access to always, and then get back to the app again, I immediately get:

---<+50.06689474,+19.92923662> +/- 165.00m (speed -1.00 mps / course -1.00) @ 28.05.2015, 13:00:39 Czas środkowoeuropejski letni

But once it is printed there is still: ---nil. Why?

This is how I setup locationManager:

private let locationManager = CLLocationManager()

in viewDidLoad() I call the method:

private func setupLocationManager() {
    locationManager.delegate = self
    locationManager.distanceFilter = kCLDistanceFilterNone
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.startUpdatingLocation()
}

NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: Selector("printLocation"), userInfo: nil, repeats: true)
Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358

1 Answers1

0

The answer how to implement CoreLocation framework in the simplest way in Swift is:

  1. import CoreLocation in the file when you use it

  2. Add framework to Link binary with libraries:

enter image description here

  1. setup private let locationManager = CLLocationManager() and implement CLLocationManagerDelegate with methods:

    func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    
        if status == .AuthorizedAlways || status == .AuthorizedWhenInUse {
            locationManager.startUpdatingLocation()
        }
    }
    
  2. In viewDidLoad setup your locationManager property:

    private func setupLocationManager() {
        locationManager.delegate = self
        locationManager.distanceFilter = kCLDistanceFilterNone
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
    
        if CLLocationManager.locationServicesEnabled() {
            locationManager.startUpdatingLocation()
        }
    
        if CLLocationManager.authorizationStatus() == .NotDetermined {
            locationManager.requestAlwaysAuthorization()
        }
    }
    
  3. most important!!!! Setup your info.plist file with key NSLocationAlwaysUsageDescription and example value: Turn on your location, we need it:-)

enter image description here

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358