I’m attempting to construct my first utility in Swift utilizing
SwiftUI and CoreLocation. That is my LocationManager observable class:
import Basis
import CoreLocation
import SwiftUI
@Observable class LocationManager: NSObject, CLLocationManagerDelegate {
let supervisor: CLLocationManager
var lastLocation: CLLocation? {
supervisor.location
}
override init() {
supervisor = CLLocationManager()
tremendous.init()
supervisor.delegate = self
supervisor.desiredAccuracy = kCLLocationAccuracyBest
}
}
extension LocationManager {
non-public func redirectToSettings() {
if let url = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(url)
}
}
func requestWhenInUseAuthorization() {
return supervisor.requestWhenInUseAuthorization()
}
non-public func handleAuthorizationStatusChange(_ standing: CLAuthorizationStatus) {
swap standing {
case .notDetermined:
print("not decided")
requestWhenInUseAuthorization()
case .authorizedWhenInUse:
print("sure")
supervisor.requestAlwaysAuthorization()
return supervisor.startUpdatingLocation()
case .authorizedAlways:
return supervisor.startUpdatingLocation()
case .denied, .restricted:
return redirectToSettings()
@unknown default:
print("Unknown authorization standing")
}
}
func locationManager(_ supervisor: CLLocationManager, didFailWithError error: Error) {
print("(error)")
}
func locationManager(_ supervisor: CLLocationManager, didChangeAuthorization standing: CLAuthorizationStatus) {
handleAuthorizationStatusChange(standing)
}
func locationManager(_ supervisor: CLLocationManager, didUpdateLocations areas: [CLLocation]) {
print(areas)
}
}
and that is the view code:
import SwiftUI
struct FirstView: View {
@Surroundings(LocationManager.self) var locationManager
var physique: some View {
if let clLocation = locationManager.lastLocation {
Textual content(clLocation.description)
} else {
Textual content("No location")
}
}
}
#Preview {
FirstView()
}
and that is the ContentView:
struct ContentView: View {
var physique: some View {
TabView {
FirstView()
.tabItem {
Label("Discover", systemImage: "globe")
}
SecondView()
.tabItem {
Label("Tortoise", systemImage: "tortoise")
}
ThirdView()
.tabItem {
Label("Tortoise", systemImage: "tortoise")
}
}
}
}
#Preview {
ContentView()
}
The issue is: when the authorization callback will get referred to as, the standing worth is .notDetermined, so I name request when in use to immediate the person to vary the authorization standing. The perform will get referred to as however the standing doesn’t change (the person will not be prompted) subsequently the callback is not referred to as a second time. Within the privateness part of the settings the placement companies are enabled. What might be the issue?