繁体   English   中英

无法在iPhone X Swift中获取当前位置

[英]Cant get current location in iPhone X Swift

我正在尝试获取当前位置。 我的GPS正在工作,并且所有iOS设备中的当前位置都需要iPhone X吗? 为什么我不仅仅在iPhone X中获得当前位置? 我正在使用iPhone X模拟器进行测试,这是模拟器错误还是必须对代码进行任何更改?

这是我的代码。 希望你理解我的问题。 提前致谢。

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Description</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>we want to know where you are!</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>we want to get your location</string>


extension NotificationCenterViewController : CLLocationManagerDelegate {
  func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
      if status == .authorizedWhenInUse {
          locationManager.requestLocation()
      }
   }

   func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

      let userLocation: CLLocation = locations[0]
      locationManager.stopUpdatingLocation()

      let latitude = userLocation.coordinate.latitude
      let longitude = userLocation.coordinate.longitude

   }

   func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
      // Why I am getting this error in only in iPhone X only.
      print("error:: (error)")
   }
}

我有同样的问题,但解决方案非常简单,请从模拟器iPhone X删除您的应用,然后再次重写。

您是否在代码中的任何地方调用了locationManager.startLocationUpdates()函数? 如果不是,则在获得.authorizedWhenInUse状态时,在已分配位置管理器或在didChangeAuthorization调用它的位置添加此行。

需要调用locationManager.startLocationUpdates()来获取位置更新的数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM