我需要获取用户的位置。
我已经在info.plist中包含了所有三个键
在视图控制器中,我已经声明了变量:
// Used to start getting the users location
var locationManager = CLLocationManager()
var currentLocation: CLLocation!
我用CLLocationManagerDelegate
扩展了视图控制器
class HomeViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UISearchBarDelegate, CLLocationManagerDelegate
而且我已经将这段代码包含在viewDidLoad中:
locationManager.requestWhenInUseAuthorization()
if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse) || (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways) {
currentLocation = locationManager.location
print(currentLocation.coordinate.latitude)
print(currentLocation.coordinate.longitude)
}
要求用户授予许可,但未收到坐标。
我在做什么错?
您必须调用startupdatinglocation()https://developer.apple.com/documentation/corelocation/cllocationmanager/1423750-startupdatinglocation才能接收CLLocationManagerDelegate的消息
您可以在https://fluffy.es/current-location/中查看完整的教程