【发布时间】:2015-02-25 05:32:38
【问题描述】:
我正在使用 iOS SDK 8.1 尝试调用 requestWhenInUseAuthorization() 方法来提示用户授予对我的应用程序的访问权限。我导入了 CoreLocation.framework,并将 NSLocationWhenInUseUsageDescription 和 NSLocationAlwaysUsageDescription 键添加到 info.plist 中。当我运行该应用程序时,它从未提示我访问位置。下面是我的代码,我错过了什么?
import UIKit
import CoreLocation
import MapKit
class ViewController: UIViewController, CLLocationManagerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
var manager = CLLocationManager()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
let authorizationStatus = CLLocationManager.authorizationStatus()
switch authorizationStatus {
case .Authorized:
println("authorized")
case .AuthorizedWhenInUse:
println("authorized when in use")
case .Denied:
println("denied")
case .NotDetermined:
println("not determined")
case .Restricted:
println("restricted")
}
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let location = locations[0] as CLLocation
println("Latitude: \(location.coordinate.latitude). Longitude: \(location.coordinate.longitude).")
}
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
switch status {
case .Authorized:
println("authorized")
case .AuthorizedWhenInUse:
println("authorized when in use")
case .Denied:
println("denied")
case .NotDetermined:
println("not determined")
case .Restricted:
println("restricted")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
控制台只显示“未确定”一次,没有其他任何内容。所以我去了 iPhone 模拟器 => 设置 => 隐私 => 位置 => 我的应用程序。它向我展示了 3 个选项:“从不”、“在使用应用程序时”、“始终”。但是什么都没有选择。
【问题讨论】:
标签: swift ios8 core-location