iOS 8 - 无法获取当前位置,错误域=kCLErrorDomain,代码=0

4
我遇到了获取当前位置的问题。仍然收到错误消息:
Error Domain=kCLErrorDomain Code=0 "The operation couldn’t be completed. (kCLErrorDomain error 0.)"

我已经按照以下步骤完成了所有操作:

在iOS模拟器中重置内容和设置(模拟器中的地图应用程序显示正确的位置)。
在Xcode中,转到Product>Scheme>Edit,取消勾选Allow Location Simulation(当我勾选它时,它会模拟例如伦敦的位置)。
在我的项目的Info.plist文件中添加NSLocationWhenInUseUsageDescription(应用程序请求位置权限,并且我可以在iOS模拟器的设置中看到它们)。
我已经打开了Wi-Fi连接(就像我说的,位置服务在Safari、Maps甚至iOS模拟器中的Maps中都可以正常工作)。

-(void)viewDidLoad    {
       [super viewDidLoad];
       self.locationManager = [[CLLocationManager alloc] init];

       self.locationManager.delegate = self;
       if(IS_OS_8_OR_LATER){
           NSUInteger code = [CLLocationManager authorizationStatus];
           if (code == kCLAuthorizationStatusNotDetermined && ([self.locationManager    respondsToSelector:@selector(requestWhenInUseAuthorization)])) {

               if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"]) {
                   [self.locationManager  requestWhenInUseAuthorization];
               } else {
                   NSLog(@"Info.plist does not contain NSLocationAlwaysUsageDescription or    NSLocationWhenInUseUsageDescription");
               }
           }
       }
       [self.locationManager startUpdatingLocation];
       [self.FirstMap setShowsUserLocation:YES];    }

   - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error    {
       NSLog(@"didFailWithError: %@", error);
       UIAlertView *errorAlert = [[UIAlertView alloc]
                                  initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil    cancelButtonTitle:@"OK" otherButtonTitles:nil];
       [errorAlert show];    }

我有没有忘记什么东西? 谢谢!


https://dev59.com/n1wY5IYBdhLWcg3wgX-V - Lal Krishna
3个回答

5

我猜你可能需要先阅读这个帖子,因为它有很大的重叠部分。

位置管理器错误:(KCLErrorDomain错误0)

但无论如何,我测试了你的代码,并得到了与你报告的相同的错误。然后我更改了仿真配置。具体来说,在模拟器中,我点击“调试” -> “位置” -> “自定义位置...”并输入一个坐标,我可以定期接收位置事件(每秒钟样本速度)。所以问题可能不是来自代码,而是来自模拟器本身。希望我的答案能够帮助您。

更新

我猜我误解了你的问题。你想要的是在模拟器中“自动”获取你的当前位置。恐怕在xcode中没有这样的功能,相反,你需要使用“将gpx文件添加到项目”,这比使用自定义位置更麻烦。但我的意思是,如果你已经成功地获得了正确的位置,为什么还要在模拟器中麻烦地获取正确的当前位置,或者换个方法,也许你可以在手机上进行测试?


就像我所说的那样,当我指定位置,例如伦敦时,它会正确显示。模拟器中地图应用程序的位置服务也可以正常工作(因此模拟器可以获取位置)。 - Mateusz Tylman
哦,我不知道在iOS模拟器中无法获取当前位置。我以为如果模拟器上的地图应用程序显示正确(与Mac上的地图应用程序相同),就有一种方法可以显示它。谢谢! - Mateusz Tylman

2

iOS 8 版本中,你需要先向用户请求位置权限才能访问该位置,与 iOS 7 不同。

if CLLocationManager.locationServicesEnabled() {
        if self.manager.respondsToSelector("requestWhenInUseAuthorization") {
            manager.requestWhenInUseAuthorization()
        } else {
            startUpdatingLocation()
        }
    }

更多信息请参考此链接http://nshipster.com/core-location-in-ios-8/

(注:该链接为关于iOS 8中核心定位的技术文章)

1

检查网络连接。 如果连接正常,则首先转到Xcode日志栏并设置“不模拟位置”。然后选择模拟器菜单>调试>位置>设置自定义或选择任何位置。


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