【问题标题】:CLLocationManager not calling delegateCLLocationManager 不调用委托
【发布时间】:2013-03-25 20:34:07
【问题描述】:

我正在开发一个 Mac 应用程序。以下是一个最小化的测试用例。似乎 CLLocationManager 没有调用任何委托方法。你看出什么不对了吗?

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>

@interface Delegate : NSObject <CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (nonatomic) BOOL once;
@end

@implementation Delegate;
@synthesize locationManager = _locationManager;
@synthesize once = _once;

- (void)start:(BOOL)once
{
    self.once = once;
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.locationManager.delegate = self;
    [self.locationManager startUpdatingLocation];
    NSLog(@"Location: %@", self.locationManager.location);
    NSLog(@"authorizationStatus: %d", [CLLocationManager authorizationStatus]);
    NSLog(@"locationServicesEnabled: %d", [CLLocationManager locationServicesEnabled]);
    NSLog(@"significantLocationChangeMonitoringAvailable: %d", [CLLocationManager significantLocationChangeMonitoringAvailable]);
    NSLog(@"headingAvailable: %d", [CLLocationManager headingAvailable]);
    NSLog(@"regionMonitoringAvailable: %d", [CLLocationManager regionMonitoringAvailable]);
}

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation;
{
    printf ( "%s\n", [newLocation.description UTF8String]);
    if (self.once) exit(0);
}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error
{
    printf ( "ERROR: %s\n", [[error localizedDescription] UTF8String]);
    exit(1);
}
@end

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        BOOL once = (argc > 1 && strcmp(argv[1], "--once") == 0);
        Delegate *delegate = [[Delegate alloc] init];
        [delegate performSelectorOnMainThread:@selector(start:) withObject:[NSNumber numberWithBool:once] waitUntilDone:NO];
        NSRunLoop *runLoop = [NSRunLoop mainRunLoop];
        [runLoop run];
    }
    return 0;
}

现在的输出是:

2013-03-25 20:24:28.342 CoreLocationCLI[75031:303] Location: (null)
2013-03-25 20:24:28.344 CoreLocationCLI[75031:303] authorizationStatus: 0
2013-03-25 20:24:28.345 CoreLocationCLI[75031:303] locationServicesEnabled: 1
2013-03-25 20:24:28.345 CoreLocationCLI[75031:303] significantLocationChangeMonitoringAvailable: 0
2013-03-25 20:24:28.346 CoreLocationCLI[75031:303] headingAvailable: 0
2013-03-25 20:24:28.346 CoreLocationCLI[75031:303] regionMonitoringAvailable: 1

更新:现在已实施并正在运行。在https://github.com/fulldecent/corelocationcli实现位置打印

【问题讨论】:

  • 您的设备/机器是否支持定位服务?是否启用?应用在设备上是否有权限?
  • NSLog(@"%@", self.locationManager.location) 和 CLLocation 的各种属性怎么样?
  • 支持,我已添加日志显示
  • 这是在主线程中吗?
  • 还尝试查看您的 locationManager 在初始化后是否被释放。

标签: objective-c macos cllocationmanager


【解决方案1】:

在调用startUpdatingLocation 之前检查+ (BOOL)locationServicesEnabled 的值。

如果它关闭,请打开系统设置中提供的位置服务。

【讨论】:

  • 这些已启用。我已更新问题以记录并显示此内容。
【解决方案2】:

授权状态:0

这只是意味着您的授权是 oFF 。请确保它已打开。 - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status

【讨论】:

    【解决方案3】:

    将 kCLAuthorizationStatusAuthorizedAlways 或 info.plist 中的 kCLAuthorizationStatusAuthorizedWhenInUse 属性

    【讨论】:

      猜你喜欢
      • 2014-09-20
      • 2011-03-12
      • 2011-11-29
      • 1970-01-01
      • 2016-05-07
      • 1970-01-01
      • 1970-01-01
      • 2015-02-01
      • 1970-01-01
      相关资源
      最近更新 更多