问题描述
我已经在我正在开发的 iOS 应用程序中成功设置了位置服务(用户可以在移动滑块后选择允许位置服务)但我的代码都没有尝试获取用户的当前位置(按长/纬度)正在工作。可以帮助找出可能出了什么问题...谢谢!
import UIKit
import MapKit
import CoreLocation
class SignUpSettingsViewController: UIViewController,CLLocationManagerDelegate {
@IBOutlet weak var locationOn: UISwitch!
var locationManager: CLLocationManager?
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func locationOn(_ sender: UISwitch) {
if (sender.isOn == true){
print("Location SLIDER TURNED ON")
locationManager = CLLocationManager()
locationManager?.delegate = self
locationManager?.requestWhenInUseAuthorization()
locationManager?.startUpdatingLocation()
func locationManager(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation]) {
guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
print("locations = \(locValue.latitude) \(locValue.longitude)")
}
func locationManager2(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation]) {
if let location = locations.last {
print(location.coordinate.latitude)
print(location.coordinate.longitude)
}
}
}
else {
print ("Location SLIDER NOT ON")
}
}
解决方法
再看一遍。
您在 func locationManager(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation])
中定义了 @IBAction func locationOn(_ sender: UISwitch)
将其移入 class SignUpSettingsViewController
您在符合 CLLocationManager 委托方面犯了错误。 UIViewController 或其他类应该将委托实现为协议 f.e.通过扩展或在类声明后添加 CLLocationManagerDelegate 。尝试将您的文件代码替换为:
import UIKit
import MapKit
import CoreLocation
// MARK: - Location manager delegate
extension SignUpSettingsViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager,didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .authorizedAlways: break
case .authorizedWhenInUse: break
case .denied,.restricted: break /// location permission denied,or updated in Settings
case .notDetermined: break /// location permission wasn't selected
@unknown default: break
}
}
func locationManager(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
print(location)
}
}
// MARK: - SignUpSettingsViewController
final class SignUpSettingsViewController: UIViewController {
@IBOutlet weak var locationOn: UISwitch!
private let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func locationOn(_ sender: UISwitch) {
if (sender.isOn == true) {
print("Location SLIDER TURNED ON")
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
locationManager.delegate = self
} else {
locationManager.delegate = nil
print ("Location SLIDER NOT ON")
}
}
}
在这个函数中你应该处理你的位置权限
func locationManager(_ manager: CLLocationManager,didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .authorizedAlways: break
case .authorizedWhenInUse: break
case .denied,or updated in Settings
case .notDetermined: break /// location permission wasn't selected
@unknown default: break
}
}
,
只需重新检查您的 info.plist 文件中需要添加的位置键即可。
另外一步是你需要保持
func locationManager(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation])
UISwitch 操作方法范围之外的方法。 我通过上述更改运行您的相同代码,它对我有用。
供您参考,请检查以下代码:
var locationManager: CLLocationManager?
@IBOutlet weak var locationOn: UISwitch!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func locationOn(_ sender: UISwitch) {
if (sender.isOn == true){
print("Location SLIDER TURNED ON")
locationManager = CLLocationManager()
locationManager?.delegate = self
locationManager?.requestWhenInUseAuthorization()
locationManager?.startUpdatingLocation()
// func locationManager(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation]) {
// guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
// print("locations = \(locValue.latitude) \(locValue.longitude)")
// }
// func locationManager2(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation]) {
// if let location = locations.last {
// print(location.coordinate.latitude)
// print(location.coordinate.longitude)
// }
// }
}
else {
print ("Location SLIDER NOT ON")
}
}
//MARK: Location service Delegate
func locationManager(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation]) {
guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
print("locations = \(locValue.latitude) \(locValue.longitude)")
}
}
祝你好运:)