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

时间:2015-09-12 21:09:35

标签: iphone cllocationmanager ios9

以下代码应获取当前位置。但是会产生上述错误。函数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”)
}

Info.plist

9 个答案:

答案 0 :(得分:44)

请试试这个:

转到:

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

编辑:正如评论中提到的那样,还要重新启动Xcode,你就完成了!

希望这有帮助。

答案 1 :(得分:3)

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

答案 2 :(得分:3)

搜索谷歌真的答案这似乎是一个问题,因为IOS4.0。

在Project =>下的XCODE 7.1中的

scheme =>编辑方案。

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

<=>在您的IOS模拟器中,在Debug =&gt;下位置选择一个位置。这解决了我的问题。

答案 3 :(得分:2)

我遇到了与我从8.0移动的应用程序相同的问题 - &gt; 9.0 / 9.1。我尝试了一些事情,比如只在Swift中编写CLLocationManager部分,以及一个新的Obj-C项目。我还在不同的设备上测试过它。为了解决设备上的问题,我删除了didFailWithError func。这完全阻止了错误。这似乎不是最好的主意,但在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];
//    }

}

.plist image

答案 4 :(得分:2)

在XCode 11.5中

打开您的模拟器:

功能=>位置=> CustomLocation

enter image description here

答案 5 :(得分:1)

从模拟器->调试->位置尝试

enter image description here

答案 6 :(得分:1)

我正在使用XCode14。这是我为解决此问题所做的事情

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

答案 7 :(得分:0)

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

  1. 不要转发模拟器的结果,因为它总是不能给出确切的结果。
  2. 用于在真实设备上进行的所有与地图相关的调试和执行测试。
  3. 与xcode 8一样(据我所知)您可以将iOS设备连接到系统并直接运行应用程序。
  4. 即使对于上述问题,解决方案也是如此,只需尝试使用真实设备,您的问题就会得到解决。

答案 8 :(得分:0)

在我的例子中,我将 CLLocationManager() 的返回值分配给了 ViewControllers viewDidLoad 覆盖中的一个变量。

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

我只需要将生命周期方法覆盖中的赋值移到我使用它的类的根目录中。