rkh
rkh

Reputation: 863

didUpdateHeading not called in swift

I am trying to get the heading information of the device in order to use the difference between the magnetic and true heading to determine the declination for the user location. For this, I have a Helper class and my MainVc (mvc). In my helper class init method I do the following:

...
...
locationManager = CLLocationManager()
switch CLLocationManager.authorizationStatus() {
  case .AuthorizedAlways:
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
    locationManager.startUpdatingLocation()
  case .NotDetermined:
    locationManager.requestAlwaysAuthorization()
  case .AuthorizedWhenInUse, .Restricted, .Denied:
    mvc.alertDenied();
}

if (!initialized)
{
  initialized = true;
  self.performSelector("finishUpdatingLocation", withObject: nil, afterDelay: 2.0);
}

}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    print ("didUpdateLocations")
    var userLocation:CLLocation = locations[0] as! CLLocation
    longitude = Float(userLocation.coordinate.longitude);
    latitude = Float(userLocation.coordinate.latitude);
  }


 func finishUpdatingLocation () {
    locationManager.stopUpdatingLocation();
    NSObject.cancelPreviousPerformRequestsWithTarget(self, selector: "finishUpdatingLocation", object: nil);
    mvc.goingToUpdateHeading();
    locationManager.startUpdatingHeading();
}

didUpdateLocations is being called and I am successfully fetching the device's coordinates. However although I have added didUpdateHeading, the first line in it is not being printed, the device's stuck at that point:

func locationManager(manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
print("didUpdateHeading");

if (newHeading.headingAccuracy > 0) {
  variation = newHeading.trueHeading - newHeading.magneticHeading;
  doneLoading = true;
  locationManager.stopUpdatingHeading();
  mvc.goingToCalculateData();

  if (calcData) {
    calculateData();
    print ("Calculating data");
    calcData = false;
  }
}
print(newHeading.magneticHeading)
}

Any ideas why the startUpdatingHeading is not being called?

Upvotes: 2

Views: 3918

Answers (2)

Varun Naharia
Varun Naharia

Reputation: 5428

If you are checking it on simulator then it will not work please check it on real device.

Upvotes: 6

Manikandan D
Manikandan D

Reputation: 1442

You have to call locationManager.startUpdatingHeading() like

...
...
locationManager = CLLocationManager()
switch CLLocationManager.authorizationStatus() {
  case .AuthorizedAlways:
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
    locationManager.startUpdatingLocation()
    locationManager.startUpdatingHeading()
  case .NotDetermined:
    locationManager.requestAlwaysAuthorization()
  case .AuthorizedWhenInUse, .Restricted, .Denied:
    mvc.alertDenied();
}

Upvotes: 4

Related Questions