didUpdateLocations从未调用过

我正在尝试获取用户的位置。 为此,我在info.plist中设置了以下属性:

在此处输入图像描述

我还在viewDidLoad方法中添加了以下代码以及下面的函数。 问题是locationManager(manager, didUpdate....)函数永远不会被调用,我也从未被提示访问位置的权限,即使我已经删除并再次安装了应用程序。 我在iPad上测试,而不是在模拟器上测试。 didFailWithError函数永远不会被调用。

  self.locationManager.delegate = self self.locationManager.desiredAccuracy = kCLLocationAccuracyBest self.locationManager.requestWhenInUseAuthorization() self.locationManager.startUpdatingLocation() func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { print("UPDATING") let locValue:CLLocationCoordinate2D = manager.location!.coordinate let latitude = locValue.latitude let longitude = locValue.longitude latitudeText = "\(latitude)" longitudeText = "\(longitude)" if let a = latitudeText, b = longitudeText { print(a) print(b) self.locationManager.stopUpdatingLocation() if (userAlreadyExist()) { dispatch_async(dispatch_get_main_queue(), { //self.performSegueWithIdentifier("segueWhenLoggedIn", sender: self) self.performSegueWithIdentifier("showCamera", sender: self) // self.performSegueWithIdentifier("showTabBarController", sender: self) }) } else { dispatch_async(dispatch_get_main_queue(), { self.performSegueWithIdentifier("segueWhenLoggedOut", sender: self) }) } } } func locationManager(manager: CLLocationManager, didFailWithError error: NSError) { print(error.localizedDescription) } 

编辑:

我添加了以下代码片段:

 if CLLocationManager.locationServicesEnabled() { print("yes") } else { print("no") } 

它返回是。 我还检查了我的设备,locationServices已启用,应用程序已列在那里,但是所有其他应用程序都在其旁边写着“While Using”,“Never”或“Always”,我没有写任何东西。

你在哪里开始更新位置? 例如:

 //location manager lazy var locationManager: CLLocationManager = { var _locationManager = CLLocationManager() _locationManager.delegate = self _locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters _locationManager.activityType = .AutomotiveNavigation _locationManager.distanceFilter = 10.0 // Movement threshold for new events _locationManager.allowsBackgroundLocationUpdates = true // allow in background return _locationManager }() if CLLocationManager.locationServicesEnabled() { locationManager.startUpdatingLocation() // start location manager } 

这是一个工作的控制器代码:对于设置自定义iOS目标属性也很重要。

将这两行添加到Info.plist:

NSLocationWhenInUseUsageDescription

NSLocationAlwaysUsageDescription

 // // ViewController.swift // LocationTest2 import UIKit import CoreLocation class ViewController: UIViewController { //location manager lazy var locationManager: CLLocationManager = { var _locationManager = CLLocationManager() _locationManager.delegate = self _locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters _locationManager.activityType = .AutomotiveNavigation _locationManager.distanceFilter = 10.0 // Movement threshold for new events // _locationManager.allowsBackgroundLocationUpdates = true // allow in background return _locationManager }() override func viewDidLoad() { super.viewDidLoad() } override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) //allow location use locationManager.requestAlwaysAuthorization() print("did load") print(locationManager) //get current user location for startup // if CLLocationManager.locationServicesEnabled() { locationManager.startUpdatingLocation() // } } } // MARK: - CLLocationManagerDelegate extension ViewController: CLLocationManagerDelegate { func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { for location in locations { print("**********************") print("Long \(location.coordinate.longitude)") print("Lati \(location.coordinate.latitude)") print("Alt \(location.altitude)") print("Sped \(location.speed)") print("Accu \(location.horizontalAccuracy)") print("**********************") } } } 

还要确保已将自定义位置设置为模拟器,因为默认情况下它将为None …在模拟器中,转到Debug – > Location->。

还应该注意的是,如果未在模拟器中设置位置,则会运行locationManager:didFailWithError:正如您所期望的那样。

您应该在didDetermineState委托方法中调用startUpdatingLocation()

  if CLLocationManager.authorizationStatus() != .AuthorizedWhenInUse { locationManager.requestWhenInUseAuthorization() }else{ locationManager.startUpdatingLocation() } 

//后来

 func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) { switch status { case .AuthorizedWhenInUse: manager.startUpdatingLocation() break case .AuthorizedAlways: manager.startUpdatingLocation() break case .Denied: //handle denied break case .NotDetermined: manager.requestWhenInUseAuthorization() break default: break } } 

对我来说工作:

 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { print("locationManager update") } 

而不是这个

 private func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { print("locationManager update") } 

我寻找答案的高低。 这是唯一对我有用的东西:将didUpdateToLocation的func更改为:

 func locationManager(_: CLLocationManager, didUpdateToLocation newLocation: CLLocation!,fromLocation oldLocation: CLLocation!) { } 

请注意“as _:CLLocationManager”的细微更改 ,而不是“manager:CLLocationManager”。