【问题标题】:Cant get current location in iPhone X Swift无法在 iPhone X Swift 中获取当前位置
【发布时间】:2018-09-29 11:25:10
【问题描述】:

我正在尝试获取我当前的位置。我的 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)")
   }
}

【问题讨论】:

  • 错误是什么?
  • 错误域=kCLErrorDomain Code=0 "(null)"
  • 谢谢。我试过了,但没有用。我的应用程序在所有设备和所有 iOS 版本的模拟器上都可以运行,除了 iPhone X。问题是什么?

标签: ios swift geolocation cllocationmanager iphone-x


【解决方案1】:

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

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

【讨论】:

  • 那是什么方法? 'CLLocationManager' 类型的值没有成员 'startLocationUpdates' ,你的意思是 locationManager.startUpdatingLocation() 吗?
  • 是的,locationManager.startUpdatingLocation()这个方法需要调用。
  • 我遇到了问题。我被拒绝了位置权限。所以我得到了所有这些问题。如果用户首先拒绝权限,我们可以以编程方式请求位置权限吗?
【解决方案2】:

我有同样的问题,但解决方案很简单,从模拟器 iPhone X 中删除您的应用程序并重新重写。

【讨论】:

  • 谢谢兄弟。我找到了问题。我首先被拒绝了位置权限。所以我得到了所有这些问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-12
  • 2015-12-29
  • 2011-11-21
  • 2014-05-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多