位置管理器didUpdateLocations未被调用 [英] Location manager didUpdateLocations not being called

查看:107
本文介绍了位置管理器didUpdateLocations未被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,即使我将委托设置为视图控制器,也不会调用 didUpdateLocations 。在 info.plist 中,我将密钥隐私 - 使用时的位置用法描述设置为描述。所以我不确定我做错了什么?

For some reason didUpdateLocations is not being called, even when I set the delegate to the view controller. In info.plist I set the key Privacy - Location When In Use Usage Description to a description. So I'm not sure what I could be doing wrong?

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 并没有指出它的del自我。这就是为什么委托(自我)的方法不被调用的原因。

This method will be called after viewDidLoad, but you new created a locationManager later in viewDidAppear and not pointed its delegate to self. That's why the delegate(self)'s methods is not be called.

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

The improved but not the best way is:

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.
        }
    }

    //......
}

这篇关于位置管理器didUpdateLocations未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆