你好
我想获取用户位置,代码第一次在 didUpdateLocations 中获取位置。但在几秒钟后应用程序崩溃。
我正在使用的代码
import UIKit
import CoreLocation
class HomeViewController: UIViewController, UIScrollViewDelegate ,UICollectionViewDataSource, UICollectionViewDelegate, CLLocationManagerDelegate {
// Location Manager
var locationManager: CLLocationManager = CLLocationManager()
//MARK: Location related functions
func trackLocation(){
// Set the delegate
self.locationManager.delegate = self
// Request location authorization
self.locationManager.requestWhenInUseAuthorization()
// Request a location update
if #available(iOS 9.0, *) {
self.locationManager.requestLocation()
} else {
// Fallback on earlier versions
}
// Start location updates
self.locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
print("Found user's location: \(location)")
}
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("Failed to find user's location: \(error.localizedDescription)")
}
请让我错过什么