Xcode 调试真实 iPhone 设备使用 CLLocation 无法更新位置

Xcode Debug real iPhone device use CLLocation can not get location updated

- (void)viewDidLoad {
    [super viewDidLoad];   

    self.clm = [[CLLocationManager alloc] init];

    [_clm setDelegate:self];

    _clm.desiredAccuracy = kCLLocationAccuracyBest;

    _clm.distanceFilter = kCLDistanceFilterNone;

    [_clm requestAlwaysAuthorization];

    [_clm startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager
    didUpdateLocations:(NSArray *)locations
{
    NSLog(@"%@",locations);

    CLLocation *location = [_clm location];

    NSString *string = [NSString stringWithFormat:@"%f,%f",location.coordinate.latitude,location.coordinate.longitude];

    [_nowFiled setText:string];
}

当我启用模拟位置时,我可以更新位置。当我禁用模拟位置时,我无法更新位置。

偶尔(不总是),它会引发错误: 操作无法完成(KCLErrorDomain 错误 0)

我在 Location Manager Error : (KCLErrorDomain error 0) 找到了解决方案。

Scheme->Edit Scheme->运行->check out(Not check)"Allow Location Simulation".

问题就解决了。

当你附加一个真实的设备进行调试时:

  • 设备将在“调试”>“模拟位置”下继承位置的调试设置。
  • 如果您将此设置为 "Don't Simulate Location",则连接的设备将使用由 GPS/WiFi 返回的真实位置(如果设备有能力)。
  • 如果您选择任何其他设置(位置),连接的设备将不会使用它的真实位置,而是模拟所选位置。

如果您在产品 > 架构 > 编辑架构下禁用核心位置 "Allow Location Simulation",您将无法在首次亮相时模拟位置,但是,如果连接的设备支持位置,将使用真实的设备位置。