gpt4 book ai didi

ios - 位置管理器 didUpdateLocations 未被调用

转载 作者:行者123 更新时间:2023-11-28 08:11:59 24 4
gpt4 key购买 nike

由于某些原因 didUpdateLocations 没有被调用,即使我将委托(delegate)设置为 View Controller 。在 info.plist 中,我将键 Privacy - Location When In Use Usage Description 设置为描述。所以我不确定自己做错了什么?

import MapKit
import CoreLocation

class OptionsViewController: UIViewController, UITableViewDelegate, CLLocationManagerDelegate {
override func viewDidLoad() {
//Ask user for location
locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()

//Use users current location if no starting point set
if CLLocationManager.locationServicesEnabled() {
let locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
}

override func viewDidAppear(_ animated: Bool) {
locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error){
locationManager.stopUpdatingLocation()
print(error)
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("Did update location called?")
let locValue:CLLocationCoordinate2D = manager.location!.coordinate
print("locations = \(locValue.latitude) \(locValue.longitude)")
}
}

最佳答案

问题在这里:

  override func viewDidAppear(_ animated: Bool) {
locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()
}

此方法将在 viewDidLoad 之后调用,但您稍后在 viewDidAppear 中新创建了一个 locationManager 并且没有将其委托(delegate)指向自身。这就是为什么不调用 delegate(self) 的方法的原因。

改进但不是最好的方法是:

class OptionsViewController: UIViewController, UITableViewDelegate, CLLocationManagerDelegate {

let locationManager = CLLocationManager()

override func viewDidLoad() {
//Ask user for location
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest

//Use users current location if no starting point set
if CLLocationManager.locationServicesEnabled() {
if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse
|| CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways {
locationManager.startUpdatingLocation()
}
else{
locationManager.requestWhenInUseAuthorization()
}
}
else{
//Alert user to open location service, bra bra bra here...
}
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//... nothing need to do for location here
}

public func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == CLAuthorizationStatus.authorizedWhenInUse
|| status == CLAuthorizationStatus.authorizedAlways {
locationManager.startUpdatingLocation()
}
else{
//other procedures when location service is not permitted.
}
}

//......
}

关于ios - 位置管理器 didUpdateLocations 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43484956/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com