簡體   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