Kampai Kampai - 6 months ago 223
Swift Question

Swift + didUpdateUserLocation not getting called

I am not able to call

MKMapView
delegate method
didUpdateUserLocation
.

What I did so far:


  1. Add framework
    MapKit.framwork
    in project

  2. Import framework in view controller by
    import MapKit
    line

  3. Add key in plist file

    <key>NSLocationAlwaysUsageDescription</key>
    <true/>

  4. Code in view controller



Added delegate
didUpdateUserLocation
method to check location is updated or not but never called.

// MARK: - MapView delegate methods

func mapView(mapView: MKMapView!, regionDidChangeAnimated animated: Bool) {
// Calling...
}

func mapView(mapView: MKMapView!, didUpdateUserLocation userLocation: MKUserLocation!) {
// Not getting called
}

// MARK: - View lifeCycle methods
override func viewDidLoad() {
super.viewDidLoad()
var locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()

locationManager.startUpdatingLocation()

self.mapView.delegate = self
self.mapView.showsUserLocation = true
self.mapView.pitchEnabled = true
self.mapView.showsBuildings = true
}


I have used simulator and change simulator custom location to check whether it is called or not? The method
regionDidChangeAnimated
is called perfectly!.

The same thing works for iOS7. what additional effort is remains for Swift map location update?

Edit: Plist keys

enter image description here

Also permission alert not prompted.

Answer
  1. You have to keep the reference to CLLocationManager.
  2. You have to wait locationManager(_:didChangeAuthorizationStatus:) delegate method called, before .startUpdatingLocation() and showsUserLocation = true.
  3. According to the document: NSLocationWhenInUseUsageDescription value type is String.

Try:

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

    @IBOutlet weak var mapView: MKMapView!

    var locationManager = CLLocationManager()

    // MARK: - View lifeCycle methods
    override func viewDidLoad() {
        super.viewDidLoad()
        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.requestWhenInUseAuthorization()

        self.mapView.delegate = self
        self.mapView.pitchEnabled = true
        self.mapView.showsBuildings = true
    }

    // MARK: - LocationManager delegate methods

    func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        switch status {
        case .Authorized, .AuthorizedWhenInUse:
            manager.startUpdatingLocation()
            self.mapView.showsUserLocation = true
        default: break
        }
    }

    // MARK: - MapView delegate methods


    func mapView(mapView: MKMapView!, didUpdateUserLocation userLocation: MKUserLocation!) {
        println(userLocation)
        // Not getting called
    }

}