IOS 9 错误域=kCLErrorDomain 代码=0“(空)”

IOS 9 Error Domain=kCLErrorDomain Code=0 "(null)"

下面的代码应该获取当前位置。但是会产生上述错误。永远不会调用函数 didUpdateLocations。

运行 这在设备上 (iPhone 5s)iOS 9.1。 Info.plist 具有必需的设备功能和隐私 - 位置使用说明配置,如附图所示。请帮忙!

let locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()
    locationManager.delegate = self
    locationManager.requestAlwaysAuthorization()
    locationManager.requestLocation()
}


func locationManager(manager: CLLocationManager,
    didFailWithError error: NSError) 
    print(error.description)
}

func locationManager(manager: CLLocationManager, didUpdateLocations
      locations: [CLLocation]) {        
 print(“got location”)
}

请试试这个:

转到:

  • 产品 -> 方案 -> 编辑方案 -> 选项 -> 必须选中允许位置模拟,尝试提供默认位置,不要将其设置为 "none"

编辑 :正如评论中提到的,也重新启动 Xcode 就大功告成了!

希望对您有所帮助。

如果没有为您弹出位置授权警报,请尝试将 NSLocationAlwaysUsageDescription 作为字符串类型添加到您的 plist。此外,对 NSLocationWhenInUseUsageDescription 执行相同的操作。

我在从 8.0 -> 9.0/9.1 移动的应用程序中遇到了同样的问题。我尝试了一些事情,比如只在 Swift 中编写 CLLocationManager 部分以及一个新的 Obj-C 项目。我还在不同的设备上测试了它。为了解决设备上的问题,我删除了 didFailWithError 函数。这完全阻止了错误。这似乎不是最好的主意,但在 IOS9 中,您可以将您的应用程序限制在具有 GPS 或定位服务的设备上。 Android 已经这样做很长时间了。我还在您的 .plist 中注意到您没有为 NSLocationALwaysUsageDescription 或包含的 NSLocationWhenInUseUsageDescrtiption 属性启用权限。

仅供参考,我附上了我当前的 .plist 看起来不会触发此错误的内容以及 obj-c 控制器文件中的代码块。代码从前一个函数的末尾开始,然后注释掉了 didFailWithError 委托和 didUpdateLocations。目前这在 IOS 9.1

中成功运行
       //Get map authorization
    [CLLocationManager locationServicesEnabled];
    locationManager = [[CLLocationManager alloc]init];
    locationManager.delegate = self;
    locationManager.allowsBackgroundLocationUpdates = YES;
    if(nil == locationManager) locationManager = [[CLLocationManager alloc]init];
    if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        [locationManager requestAlwaysAuthorization];
    }
    //locationManager.distanceFilter=10;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [locationManager startUpdatingLocation];



//- (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];
//}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{


    NSLog(@"LocationUpdated: %@",[locations lastObject]);
    [self updateMapMarkers];

//    NSDate* eventDate = location.timestamp;
//    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
//    if(fabs(howRecent) < 15.0){
//        [self updateMapMarkers];
//    }

}

正在搜索 google 正确的答案这似乎是一个问题,因为 IOS4.0.

在 XCODE 7.1 下 Project => scheme => edit scheme.

在 运行 下 => 选项 => 核心位置/禁用允许位置模拟。

在你的 IOS Simulator 下的 Debug => Location select 一个位置。这解决了我的问题。

我对在他们的应用程序中使用苹果地图的人有这些建议

  1. 不要依赖模拟器的结果,因为它总是不会给出准确的结果。
  2. 所有地图相关调试和真实设备执行测试。
  3. 如 xcode 8(据我所知),您只需将 iOS 设备连接到系统,然后 运行 直接连接应用程序。
  4. 以上问题的解决方法都是一样的,用真机试试就可以解决了。

从模拟器中尝试这个 --> 调试 --> 位置

在 XCode 11.5 及更高版本中...

打开模拟器:

功能 => 位置 => 自定义位置

我正在使用 XCode 12.5/IOS 14。这就是我为解决问题所做的工作

  1. 在 XCode 中,转到菜单“产品 -> 方案 -> 编辑方案”或按“Shift+Command+<"
  2. 单击左侧窗格中的“运行”,然后单击“选项”
  3. 您会找到“核心位置”字段,确保选中“允许位置模拟”
  4. 此外,不要将“默认位置”下拉菜单保留为“None”。 Select任何城市
  5. 关闭。无需重启XCode

在我的例子中,我将 CLLocationManager() 的 return 分配给 ViewControllers viewDidLoad 覆盖内的一个变量。

一旦生命周期方法完成,对象引用就会被释放;在用户有机会回应之前关闭对话框,也会导致其他逻辑问题,这可能取决于该引用。

我只需要将分配从生命周期方法重写移到我正在使用它的 class 的根。