didUpdateLocations永远不会调用swift

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

在这里输入图像说明

我也在我的viewDidLoad方法以及下面的函数中添加了下面的代码。 问题是locationManager(manager, didUpdate....)函数永远不会被调用,我也永远不会被提示权限访问位置,即使我已经删除并重新安装了应用程序。 我正在iPad上testing,而不是在模拟器上。 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") } 

它返回是的。 我也检查了我的设备,位置服务已启用,应用程序在那里列出,但所有其他应用程序有“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("**********************") } } } 

您应该在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 } } 

我寻找答案的高低。 这是我唯一的工作:改变didUpdateToLocation的func为:

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

请注意“as _:CLLocationManager”的微妙变化 ,而不是“manager:CLLocationManager”。