我在添加locationManager.requestWhenInUseAuthorization()时遇到麻烦

时间:2020-07-02 14:33:32

标签: ios swift

我无法添加locationManager.requestWhenInUseAuthorization()

我期望的是:弹出窗口询问用户的位置权限 什么东西:什么都没显示

设置:iphone中的位置设置:从不

Plist:

LocationAlwaysAndWhenInuseUsageDescription

LocationAlwaysUsageDescription

LocationWhenInUseUsageDescription

import Foundation
import UIKit
import CoreLocation

class NoobNoobVC: BaseVC, CLLocationManagerDelegate {
    
    var locationManager: CLLocationManager!

    
    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.distanceFilter = 5
        locationManager.requestWhenInUseAuthorization()
        if CLLocationManager.locationServicesEnabled() {
            //locationManager.startUpdatingHeading()
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        locationManager.requestWhenInUseAuthorization()

    }
}

1 个答案:

答案 0 :(得分:0)

Documentation说:

每当未确定当前授权状态(requestWhenInUseAuthorization())时,您都可以致电CLAuthorizationStatus.notDetermined

如果初始授权状态不是CLAuthorizationStatus.notDetermined,则此方法不执行任何操作,也不会调用locationManager(_:didChangeAuthorization:)方法。

您必须使用authorizationStatus()来检查授权状态,然后采取适当的措施,例如-通知用户出了什么问题,...以及可以采取哪些措施。只有requestWhenInUseAuthorization(),您才能呼叫.notDetermined。此类函数被标记为已弃用,如果您的目标iOS> = 14.0,则应使用instance variant