如何解决SwiftUI:尝试更改位置授权时出错
我目前使用此代码获取用户的当前位置,然后将其显示在地图上。这似乎工作正常。但是当用户按下“使用时允许”然后进入设置并将其更改为“下次询问”时,我的应用程序崩溃了。当这种情况发生时,我收到以下错误
错误:操作无法完成。 (kCLErrorDomain 错误 1。)
和
CLLocationManager(
我没有很多经验,但我怀疑问题是由于第 3 行造成的。我想我应该使用 @StateObject。但我可能完全错了。
如果它不是第 3 行,它可能来自我评论 Checking authorization status...
及以下的地方。但老实说我不知道
这是我收到的第二个错误/崩溃
对问题出在什么地方有什么想法吗?
顺便说一句,我已经将所有必要的“东西”添加到我的 info.plist
struct Home: View{
@State var tracking : MapUserTrackingMode = .follow
// This line Could be the issue
@State var manager = CLLocationManager()
@StateObject var managerDelegate = locationDelegate()
var body: some View {
vstack{
Map(coordinateRegion: $managerDelegate.region,interactionModes: .all,showsUserLocation: true,userTrackingMode: $tracking,annotationItems: managerDelegate.pins) { pin in
MapPin(coordinate: pin.location.coordinate,tint: .red)
}.edgesIgnoringSafeArea(.all)
}.onAppear{
manager.delegate = managerDelegate
manager.distanceFilter = kCLdistanceFilterNone
print("On Appear")
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.activityType = .automotiveNavigation
}
.alert(isPresented: $managerDelegate.locationPermissionDenied,content: {
Alert(title: Text("Permission Denied"),message: Text("Please Enable Permission In App Settings"),dismissButton: .default(Text("Go To Settings"),action: {
UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
}))
})
}
}
class locationDelegate: NSObject,ObservableObject,CLLocationManagerDelegate{
@Published var pins : [Pin] = []
@Published var location: CLLocation?
@State var hasSetRegion = false
@Published var locationPermissionDenied = false
@Published var region = MKCoordinateRegion(
center: CLLocationCoordinate2D(latitude: 38.898150,longitude: -77.034340),span: MKCoordinateSpan(latitudeDelta: 1,longitudeDelta: 1)
)
// Checking authorization status...
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
switch manager.authorizationStatus {
case .denied :
// Alert
locationPermissionDenied = true
print("Denied")
print(locationPermissionDenied)
case .restricted:
print("restricted")
case .notDetermined:
// Request
print("not Determined")
manager.requestWhenInUseAuthorization()
case .authorizedWhenInUse :
print("Authorized when in use")
manager.allowsBackgroundLocationUpdates = true
manager.startUpdatingLocation()
default:
print("Default")
}
}
func locationManager(_ manager: CLLocationManager,didFailWithError error : Error){
print("Error: \(error.localizedDescription)")
}
func locationManager(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation]) {
// pins.append(Pin(location:locations.last!))
if let location = locations.last {
print("Latitude: \(location.coordinate.latitude)")
print("Longitude: \(location.coordinate.longitude)")
self.location = location
if hasSetRegion == false{
region = MKCoordinateRegion(center: location.coordinate,span: MKCoordinateSpan(latitudeDelta: 0.001,longitudeDelta: 0.001))
hasSetRegion = true
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。