didFailWithError:错误域= kCLErrorDomain代码= 0"无法完成操作.(kCLErrorDomain error 0.)"

Luc*_*uca 79 iphone core-location cllocationmanager cllocation ios

我想获取当前位置,但我得到一个错误.

这是我的视图控制器的片段.

- (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);
}
Run Code Online (Sandbox Code Playgroud)

我期待委托方法locationManager:didUpdateLocations:被调用,但只是locationManager:didFailWithError:被调用,并打印出来:

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

sil*_*eep 133

如果您使用的是模拟器:

  1. command + shift + ,Xcode打开方案编辑器
  2. 选择"运行"方案
  3. 转到"选项"选项卡
  4. 检查✅ Allow Location Simulation
  5. 在下拉列表中选择默认位置

选择None默认位置可能会导致问题.

  • 为我工作,但我也必须做`iOS模拟器`>`重置内容和设置...` (7认同)
  • 哇!谢谢,我已经追了好几个星期了.我的应用程序一直强迫我的iPad或iPhone到英国伦敦的位置,我无法弄清楚原因.我关闭了这个选项,问题就消失了.也许是在打瞌睡时抓住了位置然后关闭并将位置更改为Scheme上的位置. (4认同)

小智 105

  1. 检查您确实拥有有效的WiFi和3G连接

    ......如果你这样做,那么:

  2. 转到设置并重置您的位置服务

  3. 重置您的网络设置

这应该解决这个问题.它与设备/网络相关而非应用相关.这很烦人,特别是如果你的应用程序依赖于位置服务而且设备只是WiFi而且人们在AppStore上给予负面评价...原谅我,我在那里有点情绪化.

  • MeM没有情绪化,这是好开发者的标志 (31认同)
  • 在我的设备(iPhone 5S,iOS 8.1.3)上玩MapKit和CoreLocation时,我遇到了完全相同的问题.我无法获得一个位置,并再次得到相同的错误(尽管移动网络信号良好).直到我打开WiFi,一切都修好了!很奇怪......谢谢! (3认同)
  • 据我所知,GPS不需要互联网或wifi连接.只是一个接收器,如果它在2或3个GPS卫星的可访问位置,我们可以得到地理位置坐标.那么为什么错误来了,如果有没有互联网或wifi连接? (2认同)
  • 指出重置位置服务和网络设置的一种支持 (2认同)
  • 您不应该经历这些步骤.只需忽略此错误并等待更多更新.请参阅下面的答案或查看文档,网址为https://developer.apple.com/library/ios/documentation/corelocation/reference/cllocationmanagerdelegate_protocol/CLLocationManagerDelegate/CLLocationManagerDelegate.html#//apple_ref/occ/intfm/CLLocationManagerDelegate/locationManager:didFailWithError : (2认同)
  • @ DuraiAmuthan.H iPad没有实际的GPS硬件.他们使用wifi MAC地址数据库来确定您的位置. (2认同)

Ray*_*ite 22

只需忽略此错误并等待更多更新或其他错误.

位置管理员的文档说这个错误是暂时的.它让你知道它无法立即检索一个位置,但它继续尝试."在这种情况下,您可以简单地忽略错误并等待新事件." 使用名为的方法是一种非常愚蠢的方式locationManager:didFailWithError:- 谢谢Apple!

Apple Documentation


Ian*_*Ian 18

尝试重新启动模拟器(假设您正在使用它).

在尝试了其他一切后,这对我有用.


Bra*_*rke 14

重新启动模拟器对我不起作用.

我不得不通过清除一切"iOS Simulator" >> "Reset Content and Settings....


rou*_*nak 9

我在模拟器上收到此错误.单击调试面板中的位置按钮并设置位置可以解决问题.(确保按钮为蓝色)

在此输入图像描述


And*_*urn 7

我也有同样的问题.我相信这篇SO帖子中涵盖了可能的解释/修复.


Rap*_*fre 5

更改模拟器上的"位置"对我有用.

调试>位置>(我的选中没有,而不是城市自行车骑行例如)


Ada*_*hns 5

  1. 在模拟器中,转到设置>常规>重置>重置位置和隐私

  2. 退出模拟器并再次运行应用程序