DidUpdateHeading not called

I want to use a compass to update the title. But my didUpdateHeading did not call. I am new to iOS. Please help so that any help is apperciated.

@interface ViewController : UIViewController<CLLocationManagerDelegate> @property (nonatomic, retain) CLLocationManager *locationManager; locationManager=[[CLLocationManager alloc] init]; locationManager.desiredAccuracy = kCLLocationAccuracyBest; locationManager.delegate=self; //Start the compass updates. [locationManager startUpdatingHeading]; - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading { NSLog(@"New magnetic heading: %f", newHeading.magneticHeading); NSLog(@"New true heading: %f", newHeading.trueHeading); } 
+5
source share
3 answers

The code you provided (assuming that you did not change anything during the insertion) is missing a function or code block to run the first part, I suggest you put it in your viewController init, for example:

 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { locationManager=[[CLLocationManager alloc] init]; locationManager.desiredAccuracy = kCLLocationAccuracyBest; locationManager.delegate=self; //Start the compass updates. [locationManager startUpdatingHeading]; } return self; } 
+2
source

You may have forgotten the delegate in .h flie <CLLocationManagerDelegate>

0
source

Are you requesting permission to use the location? You have to use

 [locationManager requestWhenInUseAuthorization]; 

or

 [locationManager requestAlwaysAuthorization]; 
0
source

All Articles