gpt4 book ai didi

ios - 在我调用 requestWhenInUseAuthorization() 之前触发位置权限对话框

转载 作者:行者123 更新时间:2023-12-01 16:05:07 24 4
gpt4 key购买 nike

当我的观点出现时:

struct TurnOnLocation: View {

var body: some View {
ZStack {
Color.orange
.edgesIgnoringSafeArea(.all)
VStack {
Spacer()
Image(systemName: "globe")
.resizable()
.aspectRatio(contentMode: .fill)
.foregroundColor(.white)
Spacer()

Button(action: {
let locationManager = LocationManager()
locationManager.locationManager.requestWhenInUseAuthorization()
locationManager.locationManager.startUpdatingLocation()
}) {
Text("Turn on location")
}.foregroundColor(Color.black)
.font(Font.system(size:22))
.padding(20)
.background(
Rectangle()
.fill(Color.white)
.border(Color.white, width:1)
)
.cornerRadius(50)
.shadow(radius: 10)
.padding(50)
}
}
}
}
它在 Button 之前询问用户位置。被轻拍。为什么是这样?我在下面添加了我的模型,以便在初始化时触发(应该是在单击按钮时)。
class LocationManager: NSObject, ObservableObject {
@Published var locationStatus: CLAuthorizationStatus? = CLAuthorizationStatus.notDetermined
@Published var locationPermissionStatus = PermissionStatus.unknown
@Published var location: CLLocation?
let locationManager = CLLocationManager()

override init(){
super.init()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
}

enum PermissionStatus {
case unknown
case authorizedWhenInUse
case authorizedAlways
case restricted
case denied
}

func getLocation() -> CLLocationCoordinate2D? {
return self.location?.coordinate
}

}
知道如何确保对话框仅在单击按钮后显示吗?
编辑:
我已经更新了代码:
Button(action: {
let locationManager = LocationManager()
locationManager.requestPermission()
})
型号
class LocationManager: NSObject, ObservableObject , CLLocationManagerDelegate {
@Published var locationStatus: CLAuthorizationStatus? = CLAuthorizationStatus.notDetermined
@Published var locationPermissionStatus = PermissionStatus.unknown
@Published var location: CLLocation?
let locationManager = CLLocationManager()

override init(){
super.init()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
}

func requestPermission(){
self.locationManager.requestWhenInUseAuthorization()
}

func locationManager(_ manager: CLLocationManager,
didChangeAuthorization status: CLAuthorizationStatus) { switch status {
case .restricted, .denied:
// Disable your app's location features
print("restricted")
break

case .authorizedWhenInUse:
// Enable your app's location features.
print("authorizedWhenInUse")
self.locationManager.startUpdatingLocation()
break

case .authorizedAlways:
// Enable or prepare your app's location features that can run any time.
print("authorizedAlways")
self.locationManager.startUpdatingLocation()
break

case .notDetermined:
print("notDetermined")
break
}
}
但是,当我单击该按钮时,询问位置权限的对话框会在一秒钟后消失。知道为什么吗?

最佳答案

有两点需要修复:

  • 您已经调用了requestWhenInUseAuthorizationinit ,这就是您在构建按钮时看到警报的原因。您应该删除 requestWhenInUseAuthorizationinit .
  • 您应该调用 startUpdatingLocation在你确定你有授权后,在 LocationManager .

  • func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    switch status {
    case .authorizedAlways, .authorizedWhenInUse:
    manager.startUpdatingLocation()
    default:
    print("no auth")
    }
    }

    关于ios - 在我调用 requestWhenInUseAuthorization() 之前触发位置权限对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63808164/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com