【问题标题】:didChangeAuthorizationStatus not called after clicking Allow单击允许后未调用 didChangeAuthorization 状态
【发布时间】:2017-04-18 12:18:14
【问题描述】:

这是我实现谷歌地图和CLLocationManager的代码:

class MapViewController: UIViewController {
    @IBOutlet weak var MapView: GMSMapView!
    var locationmanager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        locationmanager.delegate = self
        locationmanager.requestWhenInUseAuthorization()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
extension MapViewController: CLLocationManagerDelegate {
    private func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
     print("didChangeAuthorizationStatus")
     if status == .authorizedWhenInUse {
            locationmanager.startUpdatingLocation()
            MapView.isMyLocationEnabled = true
            MapView.settings.myLocationButton = true
        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        print("didUpdateLocations")
        if let location = locations.first {
            MapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
            locationmanager.stopUpdatingLocation()
        }
    }
}

点击允许或不允许后didChangeAuthorizationStatus 从未调用

【问题讨论】:

  • 不是很精通 Swift(我更喜欢 Objective-C 的人),但是 private 限定符在这里不是不明智的吗?
  • @jcaron 绝对。私有方法不会暴露给 Objective-C,因此也不会暴露给核心位置。这是一个可选方法,因此编译器不会抱怨(尽管它应该是 IMO)。
  • @Amir_P 完成。谢谢=)

标签: ios swift cllocationmanager


【解决方案1】:

如果您使用的是 Swift 3,则此方法的整个签名是不正确的。

这是你需要的:

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { 
    //your code 
}

【讨论】:

  • 如果在扩展中声明,编译器应该检测到这些东西
  • @Jarryd 不幸的是,Xcode 编译器并不完美。自从这个问题发布(半年前)以来,情况可能已经发生了变化
  • 前几天我正在实现一个位置管理器,编译器抱怨在扩展中将该函数标记为私有。由于某种原因,该函数只会被调用一次。无论如何,这解决了它,谢谢!
  • 我想说点什么!我不能在这里说。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-03
  • 2021-05-13
相关资源
最近更新 更多