【发布时间】:2015-09-28 22:14:59
【问题描述】:
我正在尝试使用 CLLocationManager 以两个坐标的形式返回用户位置。
我认为我有适当的授权 (
authorizedWhenInUse),但在获取实际位置时,它返回nil。-
我的 plist 中也添加了
NSLocationWhenInUseUsageDescriptionimport UIKit import CoreLocation class ViewController: UIViewController, CLLocationManagerDelegate { // Create an instance of the CLLocationManager var locManager = CLLocationManager() // View did load function (run things inside of here) override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. locManager.delegate = self locManager.desiredAccuracy = kCLLocationAccuracyBest // Check authorizationStatus let authorizationStatus = CLLocationManager.authorizationStatus() // List out all responses switch authorizationStatus { case .AuthorizedAlways: println("authorized") case .AuthorizedWhenInUse: println("authorized when in use") case .Denied: println("denied") case .NotDetermined: println("not determined") case .Restricted: println("restricted") } // Get the location locManager.startUpdatingLocation() if( CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse){ // Extract the location from CLLocationManager let userLocation = locManager.location // Check to see if it is nil if userLocation != nil { println("location is \(userLocation)") } else { println("location is nil") } } else { println("not authorized") } } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }}
【问题讨论】: