2013-03-25 78 views
0

我正在使用Mac应用程序。以下是一个最小化的测试用例。看起来像CLLocationManager不调用任何委托方法。你看到有什么不对吗?现在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

+0

请问您的设备/机器支持定位服务?它启用了吗?该应用是否具有设备许可? – Tim 2013-03-25 21:14:55

+0

'NSLog(@“%@”,self.locationManager.location)'和CLLocation的各种属性如何? – 2013-03-25 22:00:52

+0

它被支持,我已添加日志记录以显示 – 2013-03-26 00:25:37

回答

0

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

如果它打开了在您的系统设置中服务的位置。

+0

这些已启用。我已经更新了这个问题来记录并显示这个问题。 – 2013-03-26 00:26:04

0

authorizationStatus:0

它只是意味着你有你的授权是oFF。请确保它处于开启状态。 - (空)的LocationManager:(CLLocationManager *)经理didChangeAuthorizationStatus:(CLAuthorizationStatus)状态

0

认沽kCLAuthorizationStatusAuthorizedAlways或info.plist中 kCLAuthorizationStatusAuthorizedWhenInUse性能