CLLocationManager 未请求用户许可
CLLocationManager not requesting permission from user
我正在 Swift 开发一个应用程序,它必须获取用户的 GPS 坐标。我已将密钥(NSLocationWhenInUseUsageDescription 和 NSLocationAlwaysUsageDescription)添加到 info.pList,如下所示:
<key>NSLocationWhenInUseUsageDescription</key>
<string>Location is required to find your coordinates</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Location is required to find your coordinates</string>
我还在 viewDidLoad 方法中添加了 CoreLocation.framework 并请求授权:
override func viewDidLoad() {
super.viewDidLoad()
var locManager = CLLocationManager()
locManager.requestWhenInUseAuthorization()
locManager.startUpdatingLocation()
if(CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse){
var currentLocation = locManager.location
longitude = currentLocation.coordinate.longitude
latitude = currentLocation.coordinate.latitude
textBox.text = longitude.description + latitude.description
}
}
正如标题中所说,问题是没有提示用户获得许可,因此位置永远不会更新。
首先添加这一行
locManager.delegate = 自己;
override func viewDidLoad() {
super.viewDidLoad()
locManager.delegate = self;
locManager.requestWhenInUseAuthorization()
locManager.startUpdatingLocation()
if(CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse){
var currentLocation = locManager.location
longitude = currentLocation.coordinate.longitude
latitude = currentLocation.coordinate.latitude
textBox.text = longitude.description + latitude.description
}
比在你的 class 添加 CLLocationManagerDelegate
例子
class ViewController: UIViewController, CLLocationManagerDelegate
终于改变范围
var locManager = CLLocationManager()
例如
class ViewController: UIViewController ,CLLocationManagerDelegate{
var locManager = CLLocationManager()
override func viewDidLoad() {
...
将您的 locManager 作为 ViewController 的 属性,而不是局部变量。
class YourViewController {
var locManager: CLLocationManager!
override func viewDidLoad() {
locManager = CLLocationManager()
locManager.requestWhenInUseAuthorization()
locManager.startUpdatingLocation()
...
}
}
我正在 Swift 开发一个应用程序,它必须获取用户的 GPS 坐标。我已将密钥(NSLocationWhenInUseUsageDescription 和 NSLocationAlwaysUsageDescription)添加到 info.pList,如下所示:
<key>NSLocationWhenInUseUsageDescription</key>
<string>Location is required to find your coordinates</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Location is required to find your coordinates</string>
我还在 viewDidLoad 方法中添加了 CoreLocation.framework 并请求授权:
override func viewDidLoad() {
super.viewDidLoad()
var locManager = CLLocationManager()
locManager.requestWhenInUseAuthorization()
locManager.startUpdatingLocation()
if(CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse){
var currentLocation = locManager.location
longitude = currentLocation.coordinate.longitude
latitude = currentLocation.coordinate.latitude
textBox.text = longitude.description + latitude.description
}
}
正如标题中所说,问题是没有提示用户获得许可,因此位置永远不会更新。
首先添加这一行
locManager.delegate = 自己;
override func viewDidLoad() {
super.viewDidLoad()
locManager.delegate = self;
locManager.requestWhenInUseAuthorization()
locManager.startUpdatingLocation()
if(CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse){
var currentLocation = locManager.location
longitude = currentLocation.coordinate.longitude
latitude = currentLocation.coordinate.latitude
textBox.text = longitude.description + latitude.description
}
比在你的 class 添加 CLLocationManagerDelegate 例子
class ViewController: UIViewController, CLLocationManagerDelegate
终于改变范围
var locManager = CLLocationManager()
例如
class ViewController: UIViewController ,CLLocationManagerDelegate{
var locManager = CLLocationManager()
override func viewDidLoad() {
...
将您的 locManager 作为 ViewController 的 属性,而不是局部变量。
class YourViewController {
var locManager: CLLocationManager!
override func viewDidLoad() {
locManager = CLLocationManager()
locManager.requestWhenInUseAuthorization()
locManager.startUpdatingLocation()
...
}
}