info.plist 文件:
<key>NSLocationWhenInUseUsageDescription</key>
<string>This project would like to know your location</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>location when app is in background</string>
如上所示,已经为info.plist添加了key/value对
我面临的问题是,我第一次运行该应用程序时,它提示我进行授权,但在随后的运行中,弹出窗口并未出现。
其次,我正在尝试打印位置
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
使用 print(location)
但没有打印任何内容。
在调试选项中,我将位置设置为“city bicycle ride”
我使用的代码如下。
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate , CLLocationManagerDelegate {
@IBOutlet var map: MKMapView!
//use the locManager to get the users location
var locManager = CLLocationManager()
override func viewDidLoad() {
locManager.delegate = self
locManager.desiredAccuracy = kCLLocationAccuracyBest
locManager.requestWhenInUseAuthorization()
super.viewDidLoad()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print(locations)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
最佳答案
iOS 只会请求一次位置授权。如果用户拒绝,他将不得不在设备设置中手动更改此设置。如果他同意,您将能够获取该位置,直到他在设备设置中阻止它。
所以这段代码:
locManager.requestWhenInUseAuthorization()
只会提示弹出窗口 1 次。问题是您可能忘记调用:
locManager.startUpdatingLocation()
每次要开始获取位置时都需要调用它。
关于ios - 无法看到位置 : [CLLocation],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36734672/