CLLocationManager不调用委托

时间:2013-03-25 20:34:07

标签: objective-c macos cllocationmanager

我正在处理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

实施位置打印

3 个答案:

答案 0 :(得分:0)

在致电startUpdatingLocation之前,请检查+ (BOOL)locationServicesEnabled值。

如果关闭了系统设置中所服务的位置。

答案 1 :(得分:0)

authorizationStatus:0

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

答案 2 :(得分:0)

将kCLAuthorizationStatusAuthorized一直放在OR上     kCLAuthorizationStatusAuthorizedWhenInInsese info.plist

中的属性