【问题标题】:Error when getting user location with CLLocationManager使用 CLLocationManager 获取用户位置时出错
【发布时间】:2016-02-25 23:35:30
【问题描述】:

我正在使用集合视图和集合视图可重用标题。在标题中,我试图获取用户的位置。我不断收到此错误:

   Trying to start MapKit location updates without prompting for    
   location authorization. Must call -[CLLocationManager    
   requestWhenInUseAuthorization] or -[CLLocationManager    
   requestAlwaysAuthorization] first.

我查看了这个问题:Location Services not working in iOS 8,但没有一个建议的解决方案对我有用。我已将密钥(NSLocationAlwaysUsageDescription 和 NSLocationWhenInUseUsageDescription)添加到我的 info.Plist 中,但这并没有帮助。该应用程序从不要求用户获取其位置。这是我的代码:

编辑:我现在的代码:

   import UIKit
   import MapKit
   import Parse
   import ParseUI
   import CoreLocation

   class ReusableHeaderMap: UICollectionReusableView, CLLocationManagerDelegate {
let locationManager = CLLocationManager()

@IBOutlet weak var newMap: MKMapView!


override func awakeFromNib() {
    locationManager.delegate = self
    let status = CLLocationManager.authorizationStatus()
    if status == CLAuthorizationStatus.Denied {

    }
    else if status == CLAuthorizationStatus.NotDetermined {
        self.locationManager.requestAlwaysAuthorization()
    }
    else if status == CLAuthorizationStatus.AuthorizedAlways {
        self.locationManager.startUpdatingLocation()
    }
    else {
        print("it took the else route")

    }

    print("status is \(status)")
    self.newMap.layer.cornerRadius = 8
    self.newMap.clipsToBounds = true

}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    if locations.count > 0 {
        locationManager.stopUpdatingLocation()
        let location1 = locations.first as CLLocation!
        let mapSpan = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
        let mapRegion = MKCoordinateRegion(center: location1.coordinate, span: mapSpan)
        self.newMap.setRegion(mapRegion, animated: true)
    }
}


func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {

    if status == CLAuthorizationStatus.Denied {
        print("there will be limited functionaliy")
    }
    else if status == CLAuthorizationStatus.AuthorizedAlways {
        self.locationManager.startUpdatingLocation()
    }
    else {
        print("it took the else route")
      }
   }


}

我的 info.plist 有这个键:

  <key>NSLocationAlwaysUsageDescription</key>
<string>Get User Location</string>

【问题讨论】:

  • 我假设您正在设备上进行测试。从设备中删除该应用,然后重试。
  • 我试过了,但没有用,我仍然收到同样的错误信息。
  • 你从CLLocationManager.authorizationStatus() 得到了什么价值?你的didChangeAuthorizationStatus() 方法被调用了吗?
  • 状态未确定。是的,正在调用 didCHangeAuthorizationStatus()。在那里,我有“else if status == CLAuthorizationStatus.NotDetermined { self.locationManager.requestWhenInUseAuthorization() }”,但它从来没有提示用户请求授权。

标签: ios swift cllocationmanager


【解决方案1】:

错误消息告诉您必须做什么。你需要打电话给

self.locationManager.requestWhenInUseAuthorization() 

self.locationManager.requestAlwaysAuthorization()

在您尝试启动位置管理器之前在您的代码中。该步骤是除了将 NSLocationAlwaysUsageDescription 和/或 NSLocationWhenInUseUsageDescription 键添加到 info.plist 之外。

有关更多信息,请参阅有关这些方法的文档。

(查看您发布的代码,您的设置不正确。您不能只调用requestWhenInUseAuthorization,然后立即调用startUpdatingLocation。)

正如文档所说:

当当前授权状态为 kCLAuthorizationStatusNotDetermined,此方法异步运行 并提示用户授予应用使用位置的权限 服务。

这里的关键词是异步

您需要进行该调用,然后实现locationManager:didChangeAuthorizationStatus: 方法。如果用户授予您的应用使用位置服务的权限,将调用该方法 (didChangeAuthorizationStatus)。

您的代码应该看起来更像这样:

override func awakeFromNib() 
{
  locationManager.delegate = self
  let status = locationManager.status
  if (status == kCLAuthorizationStatusNotDetermined)
  {
    locationManager.requestWhenInUseAuthorization()
  }
  else if (status == kCLAuthorizationStatusAuthorized)
  {
    locationManager.startUpdatingLocation()
  }
  else
  {
    //Handle other error cases here.
  }
  newMap.layer.cornerRadius = 8
  newMap.clipsToBounds = true
}

optional func locationManager(_ manager: CLLocationManager,
  didChangeAuthorizationStatus status: CLAuthorizationStatus)
{
  locationManager.startUpdatingLocation()
}

【讨论】:

  • 我实现了 locationManager:didChangeAuthorizationStatus,并在里面放了:self.locationManager.startUpdatingLocation()。但这仍然不起作用,我仍然在控制台中收到相同的错误消息。我正在运行 Swift 2 和 Xcode 7.0,如果有区别的话。
  • 我试过了,但还是不行。我在我的问题中编辑了我的代码。 (语法改变了,“locationManager.status”现在在 swift2 中无效,但我认为我应该做同样的事情)
猜你喜欢
  • 1970-01-01
  • 2014-08-06
  • 1970-01-01
  • 2015-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多