didFailWithError: Error Domain=kCLErrorDomain Code=0 "操作无法完成。(kCLErrorDomain错误0。)"

86

我想获取当前位置,但却收到了错误提示。

以下是我的视图控制器代码片段。

- (void)viewDidLoad {
    self.locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate:self];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
    [locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager 
     didUpdateLocations:(NSArray<CLLocation *> *)locations {
    // I would get the latest location here
    // but this method never gets called
}
- (void)locationManager:(CLLocationManager *)manager 
       didFailWithError:(NSError *)error {
    NSLog(@"didFailWithError: %@", error);
}

我期望委托方法locationManager:didUpdateLocations:被调用,但实际上只有locationManager:didFailWithError:被调用,并打印出以下内容:

didFailWithError: Error Domain=kCLErrorDomain Code=0 "The operation couldn’t be completed. (kCLErrorDomain error 0.)" 

你可以使用Google API作为替代方案。请参考我在这里的回答https://dev59.com/fIjca4cB1Zd3GeqP18xh#40670608。 - Deepak Thakur
跳转到模拟器 -> 功能 -> 位置 -> Apple。如果你将其设置为 none,它将无法工作并抛出此错误。我是无意中这样做的。 - CDM social medias in bio
19个回答

3
我之前见过这个问题,有一种方法可以解决它,但只能使用一次。如果您想再次运行该项目,则每次需要重复此解决方案。
1. 在Xcode中,选择Product -> Scheme -> Edit Scheme,然后取消“Allow Location Simulator”选项。 2. 到iOS模拟器中重置内容和设置。 3. 再次回到Xcode,在第一步中重复操作。 4. 回到iOS模拟器并再次重置。然后它就会起作用了。

3
假设您正在使用模拟器,您可以转到Debug->Location并设置位置。

如果您没有选择任何位置,则会出现此错误。


3

如果您正在使用自定义位置,请确保经纬度正确,我曾经把它们弄反了,浪费了3个小时才意识到...


小伙伴们,我也浪费了2个小时的时间,请确保经纬度信息正确无误。 - Arpit B Parekh

1

对于返回的开发者 :) 只需从模拟器菜单中选择“Debug->Location->Freeway Drive”。如果仍有问题,请尝试在模拟器菜单中执行“Simulator->Reset Content and Settings”后再尝试。这曾经帮助过我解决同样的问题。有时模拟器位置被设置为“Custom location”,因此无法检测到任何内容。


1
这种问题可以通过将位置设置为"Apple"来解决。至少在测试目的下有效。

1
  1. 我已重置内容和设置。
  2. 从衍生数据中删除了数据。
  3. 重新启动了Xcode和模拟器,对我有用。

来自@Mem和许多其他人的答案,谢谢。


0

在设备上尝试。有时模拟器无法获取您的位置。


0

在模拟器中返回主界面

主界面 -> 设置 -> 隐私 -> 定位服务 -> 选择应用并选择始终允许


0

在 info.plist 中加入隐私权限 - 使用时获取位置描述,对我来说解决了问题


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接