didUpdateHeading未调用

时间:2015-08-21 07:15:27

标签: ios objective-c iphone

我想使用指南针更新标题。但是我的didUpdateHeading没有被调用。我是iOS的新手。请帮助任何帮助。

@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);
}

3 个答案:

答案 0 :(得分:2)

您提供的代码(假设您在此处粘贴时没有更改任何内容)缺少运行第一部分的函数或块代码,我建议您将其放在viewController init中,就像这样:

- (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;
}

答案 1 :(得分:0)

也许你忘记了.h flie <CLLocationManagerDelegate>

中的代表

答案 2 :(得分:0)

您是否要求使用位置授权?你需要使用

handleIntent()

[locationManager requestWhenInUseAuthorization];