This commit is contained in:
fmodf 2024-07-04 13:54:09 +02:00
parent b309574c78
commit f679c7d357
3 changed files with 31 additions and 9 deletions

View file

@ -0,0 +1,10 @@
import UIKit
func openAppSettings() {
if
let appSettingsUrl = URL(string: UIApplication.openSettingsURLString),
UIApplication.shared.canOpenURL(appSettingsUrl)
{
UIApplication.shared.open(appSettingsUrl, completionHandler: nil)
}
}

View file

@ -9,6 +9,7 @@ struct AttachmentLocationPickerView: View {
center: CLLocationCoordinate2D(latitude: 34.011_286, longitude: -116.166_868), center: CLLocationCoordinate2D(latitude: 34.011_286, longitude: -116.166_868),
span: MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2) span: MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2)
) )
@State private var showingAlert = false
var body: some View { var body: some View {
MapView(coordinateRegion: $region) MapView(coordinateRegion: $region)
@ -20,14 +21,30 @@ struct AttachmentLocationPickerView: View {
region.center = newLocation.coordinate region.center = newLocation.coordinate
} }
} }
.onChange(of: locationManager.authorizationStatus) { newStatus in
if newStatus == .denied {
showingAlert = true
}
}
.alert(isPresented: $showingAlert) {
Alert(
title: Text("Location Permission Denied"),
message: Text("Please enable location permissions in settings."),
dismissButton: .default(Text("OK")) {
openAppSettings()
}
)
}
} }
} }
class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate { class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
private let locationManager = CLLocationManager() private let locationManager = CLLocationManager()
@Published var lastLocation: CLLocation? @Published var lastLocation: CLLocation?
@Published var authorizationStatus: CLAuthorizationStatus
override init() { override init() {
authorizationStatus = locationManager.authorizationStatus
super.init() super.init()
locationManager.delegate = self locationManager.delegate = self
} }
@ -40,6 +57,10 @@ class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
func locationManager(_: CLLocationManager, didUpdateLocations locations: [CLLocation]) { func locationManager(_: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
lastLocation = locations.first lastLocation = locations.first
} }
func locationManager(_: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
authorizationStatus = status
}
} }
struct MapView: UIViewRepresentable { struct MapView: UIViewRepresentable {

View file

@ -229,15 +229,6 @@ struct AttachmentMediaPickerView: View {
} }
} }
} }
func openAppSettings() {
if
let appSettingsUrl = URL(string: UIApplication.openSettingsURLString),
UIApplication.shared.canOpenURL(appSettingsUrl)
{
UIApplication.shared.open(appSettingsUrl, completionHandler: nil)
}
}
} }
private struct ThumbnailView: Identifiable, View { private struct ThumbnailView: Identifiable, View {