Ios 应用程序(swift)无法显示核心位置授权警报

Ios 应用程序(swift)无法显示核心位置授权警报,ios,swift,core-location,Ios,Swift,Core Location,我正在编写一个swift应用程序,遵循“iOS学徒”中的教程,在这一步中,我想使用场景中的getButton来显示访问GPS的授权警报,但它不起作用。我检查了代码,还修改了info.plist(添加了NSLocationWhenInUsageDescription),但仍然不起作用 import UIKit import CoreLocation class CurrentLocationViewController: UIViewController,CLLocationManagerDel

我正在编写一个swift应用程序,遵循“iOS学徒”中的教程,在这一步中,我想使用场景中的getButton来显示访问GPS的授权警报,但它不起作用。我检查了代码,还修改了info.plist(添加了NSLocationWhenInUsageDescription),但仍然不起作用

import UIKit
import CoreLocation

class CurrentLocationViewController: UIViewController,CLLocationManagerDelegate {

let locationManager = CLLocationManager()

@IBOutlet weak var messageLabel:UILabel!
@IBOutlet weak var latitudeLabel:UILabel!
@IBOutlet weak var longitudeLabel:UILabel!
@IBOutlet weak var addressLabel:UILabel!
@IBOutlet weak var tagButton:UIButton!
@IBOutlet weak var getButton:UIButton!

@IBAction func getLocation() {
    let authStatus:CLAuthorizationStatus = CLLocationManager.authorizationStatus()
    if authStatus == .NotDetermined {
        locationManager.requestWhenInUseAuthorization()
        return
    }

    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
    locationManager.startUpdatingLocation()
}

// MARK: - CLLocationManagerDelegate
func locationManager(manager:CLLocationManager!,didFailWithError error:NSError!) {
    print("didFailWithError \(error)")
}

func locationManager(manager:CLLocationManager!,didUpdateLocations locations:[AnyObject]!) {
    let newLocation = locations.last as! CLLocation
    print("didUpdateLocations \(newLocation)")
}

再次检查您的plist密钥。也许这只是你问题中的一个输入错误。应该是
nslocationwhenusagekey
,但我认为您真正的问题是在请求whenusagekey之后返回。这将导致未设置您的代理,如果未设置代理,您将无法知道用户何时已批准。此外,您正在尝试
startUpdatingLocation
,即使authStatus不是已批准的状态之一。。。您缺少didChangeAuthStatus委托方法…请仔细检查您的plist密钥。也许这只是你问题中的一个输入错误。应该是
nslocationwhenusagekey
,但我认为您真正的问题是在请求whenusagekey之后返回。这将导致未设置您的代理,如果未设置代理,您将无法知道用户何时已批准。此外,您正在尝试
startUpdatingLocation
,即使authStatus不是已批准的状态之一。。。您缺少didChangeAuthStatus委托方法。。。