Swift locationManager.requestWhenInUseAuthorization() 不提示用户

问题描述 投票:0回答:1
locationManager.requestWhenInUseAuthorization()

当用户按下按钮时调用,但不显示提示。

这是我的完整代码

import SwiftUI
import CoreLocation



struct ContentView: View {
    @ObservedObject var distanceTracker = DistanceTracker();
    var body: some View {

        Button(action: distanceTracker.enableLocServices){
            Text("Enable Location")
        }
        
    }


}



struct ContentView_Previews: PreviewProvider {

    static var previews: some View {

        ContentView()

    }

}


import CoreLocation

class DistanceTracker: NSObject, ObservableObject, CLLocationManagerDelegate {
    
    let locationManager = CLLocationManager()
    var startLocation: CLLocation!
    var lastLocation: CLLocation!
    @Published var traveledDistance = 0.0
    
    override init() {
        super.init()
        locationManager.delegate = self
    }
    
    func enableLocServices(){
        locationManager.requestWhenInUseAuthorization()
        print("bruh")
    }
    
    func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
        switch manager.authorizationStatus {
        case .notDetermined:
            print("Location permission not asked for yet")

        case .restricted:
            print("Location usage is restricted (perhaps by parental controls or some other restriction)")
        case .denied:
            print("Location permission denied by user")
        case .authorizedAlways:
            print("Location permission granted for background and foreground use")
        case .authorizedWhenInUse:
            print("Location permission granted for foreground use only")
        @unknown default:
            print("An unknown authorization status received")
        }
    }
}

按下按钮时,它会打印“bruh”。编辑:另外,打印的状态是“尚未请求位置权限”

NSLocationAlwaysAndWhenInUseUsageDescription
NSLocationWhenInUseUsageDescription
均添加到信息部分。

手表和手机上的位置服务均已启用(这是一个仅限 watchOS 的应用程序,但仍然如此,无论如何我都无法让它在 IOS 上工作),并且之前未曾拒绝过该权限。我怎样才能解决这个问题?谢谢!

ios swift location core-location watchos
1个回答
1
投票

您正在使用这个

Privacy - Location Always and When In Use Usage Description

您还需要添加以下内容:

Privacy - Location When In Use Usage Description

当您尝试执行此操作时,您将看到弹出的权限。

© www.soinside.com 2019 - 2024. All rights reserved.