didFailWithError:错误域= kCLErrorDomain代码= 0“操作无法完成。(kCLErrorDomain错误0.)”

问题描述:

我想获取当前位置,但我得到一个错误。didFailWithError:错误域= kCLErrorDomain代码= 0“操作无法完成。(kCLErrorDomain错误0.)”

这是我的视图控制器的一个片段。

- (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.)" 
+0

您可以使用谷歌API作为替代。请参阅我的答案在这里http://*.com/a/40670608/2033377 – 2016-11-18 06:25:54

1) check that you actually have a valid WiFi and 3G connection

如果你这样做,然后

2) go to settings and reset your location services
3) reset your network settings

这应该照顾这个问题的是相关设备/网络应用不相关。它的烦人,尤其是如果你的应用程序是依赖于位置服务和设备是WiFi只有和人们对AppStore给予负面评价....对我来说我有点感动

+27

MeM不会变得情绪化,这是好开发商 – 2013-02-11 12:45:26

+0

OMG的标志。谢谢。 – 2013-08-21 19:01:00

+2

据我所知GPS不需要互联网或无线连接。它只是一个接收器,如果它在2或3个GPS卫星的可访问位置,我们可以获得地理位置坐标。那么,为什么错误会出现没有互联网或无线连接? – 2013-12-06 07:21:03

我有同样的问题。我相信可能的解释/修复涵盖了in this SO post

如果您使用模拟器:

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

选择None作为您的默认位置可能导致了问题。

+4

哇!谢谢,我一直在追逐这个数周。我的应用程序不断强制我的iPad或iPhone到伦敦,英国的位置,我无法弄清楚为什么。我把这个选项关掉了,问题没有了。可能是打嗝抓住了那个位置,然后离开,把位置改成了计划中的位置。 – 2013-11-06 20:59:03

+1

太棒了,它为我工作;)谢谢你 – Rifinio 2014-04-21 22:25:02

+1

也为我工作。谢谢。 – 2014-10-30 09:42:14

尝试重新启动模拟器(假设这就是你正在使用的)。

尝试了其他一切后,这为我工作。

+0

完美的解决方案。 – zproject89 2014-04-12 03:35:33

+0

为我工作。谢谢 – 2014-12-24 07:06:27

+0

也在这里工作,谢谢 – VinceOPS 2016-02-11 00:27:18

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

如果你没有选择你会有这个错误。

+0

这对我工作! – holierthanthou84 2014-09-03 06:05:47

重新启动模拟器不适用于我。

I had to clear everything via "iOS Simulator" >> "Reset Content and Settings... .

+3

为我工作thnkkssssss – NullData 2014-07-26 20:00:19

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

位置管理器的文档说这个错误是暂时的。它让你知道,它没有立即检索一个位置,但它仍然在尝试。 “在这种情况下,你可以简单地忽略错误并等待新事件发生。”使用名为locationManager:didFailWithError:的方法,这是一个非常头疼的方式 - 谢谢Apple!

Apple Documentation

在模拟器上改变 “位置” 为我工作。

Debug > Location > (Mine was checked None instead of City Bicycle Ride e.g.)

像这样的问题可以通过设置位置为“Apple”来解决。至少它适用于测试目的。

我在模拟器上出现这个错误。点击调试面板中的位置按钮,并设置一个位置为我解决了这个问题。 (确保按钮是蓝色的)

enter image description here

如果您使用的是custom location请确保您有长期和纬度的正确方法,我曾经有过逆转,浪费3小时我意识到之前...

+0

艾姆在这里我也浪费了2个小时,设置了经度和纬度在正确的位置。 – 2015-07-02 09:53:14

  1. In the simulator go to Settings > General > Reset > Reset Location & Privacy

  2. Quit simulator and run app again

  1. I have reseted contents and settings.
  2. Removed data from derived data.
  3. Restarted the Xcode, and Simulator and worked for me.

从@Mem的答案和许多其他的感谢

我以前看到的问题,有解决这个问题的一种方式,但它只能工作一次。如果您想再次运行该项目,并且每次都需要重复该解决方案。

1.In Xcode,Product -> Scheme -> Edit Scheme,then cancel the "Allow Location Simulator".

2.To the iOS Simulator and reset Content and Setting.

3.Again to the Xcode,repeat the first step.

4.To the iOS Simulator and reset. Then it will work.

对于开发者返回:)刚刚从模拟器菜单中选择 “Debug->Location->Freeway Drive”。如果仍然存在问题,请在模拟器菜单中执行“模拟器 - >重置内容和设置”后尝试。这帮助我解决了同样的问题。某些时间模拟器位置设置为“Custom location”,因为它未检测到任何内容。

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

转到家里模拟器

首页 - >设置 - >高级 - >位置 - >选择应用程序,然后选择总是