如何解决为什么在swiftui位置弹出授权很快消失又重新出现?
在我的应用中,我需要获得位置许可。我的应用程序分为几个组件。在 contentView
文件中,我调用为我提供位置信息的 locationProvider
类。然后,我将这些信息之一传递到显示当前位置或当前标题的组件中。
令人担忧的是,当显示权限模式时,它出现然后很快消失。我已经查看了这方面的问题,例如 here。但是这个没有帮助,因为它有点不同;
我的contentView
:
import UIKit
import CoreLocation
public extension CGFloat {
var degreesToradians: CGFloat { return self * .pi / 180 }
}
struct ContentView: View {
@Observedobject var location: LocationProvider = LocationProvider()
private var windowWidth: CGFloat = UIScreen.main.bounds.width
@State private var heading: CGFloat = .zero
@State private var city: String = ""
var body: some View {
vstack() {
Image("arrow")
.resizable()
.aspectRatio(contentMode: .fit)
.rotationEffect(.degrees(Double(-self.heading)))
.animation(.easeInOut(duration: 0.2))
.frame(width: 300,height: 300)
.onReceive(self.location.heading) { heading in
self.heading = CGFloat(heading)
}
degrees(heading: $heading)
Location(city: $city)
}
.padding(.horizontal,20)
.padding(.vertical,40)
.onReceive(self.location.city) { city in
self.city = city
}
}
}
LocationProvider
文件:
import SwiftUI
import CoreLocation
import Combine
public class LocationProvider: NSObject,CLLocationManagerDelegate,ObservableObject {
// Location manager
private let locationManager: CLLocationManager = CLLocationManager()
// heading
public let heading = PassthroughSubject<CGFloat,Never>()
public let city = PassthroughSubject<String,Never>()
@Published var currentheading: CGFloat {
willSet { heading.send(newValue) }
}
@Published var currentCity: String {
willSet { city.send(newValue) }
}
public override init() {
currentheading = 0
currentCity = ""
super.init()
//
// Location manager
//
locationManager.delegate = self
self.setup()
}
private func setup() {
// Réglage de la préscision
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
locationManager.startUpdatingheading()
}
//
// Location manager - Update heading
// Calcule de l'angle et de la destination
//
public func locationManager(_ manager: CLLocationManager,didUpdateheading newheading: CLheading) {
self.currentheading = CGFloat(newheading.magneticheading)
}
public func locationManager(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
getLocation(from: location) { city,administrativeArea,error in
guard let city = city,error == nil else { return }
self.currentCity = city
}
}
public func getLocation(from location: CLLocation,completion: @escaping (_ city: String?,_ administrativeArea: String?,_ error: Error?) -> ()) {
CLGeocoder().reverseGeocodeLocation(location) { placemarks,error in
completion(placemarks?.first?.locality,placemarks?.first?.administrativeArea,error)
}
}
}
Location
组件文件:
import UIKit
import CoreLocation
struct Location: View {
@Binding var city: String
var body: some View {
vstack() {
Text("\(self.city)")
.font(Font.system(size: 24,weight: .thin,design: .default))
}
}
}
我知道如果我在组件中声明 @Observedobject var location: LocationProvider = LocationProvider()
行,问题就不会出现。但这不是我想要的。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。