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 上工作),并且之前并未拒绝该许可。我怎样才能解决这个问题?谢谢!