我正在尝试获取用户的位置.为此,我在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("**********************") } } }