admin管理员组文章数量:1414621
CoreLocation returns .notDetermined on Simulator even though Location Services are enabled, how can I manually trigger the authorization dialog?
In my Info.plist, I have added the necessary keys for location permissions.
eg: details about the questions ..........................................................................
import Foundation
import CoreLocation
@Observable
final class LocationManager: NSObject, CLLocationManagerDelegate {
var locationManager = CLLocationManager()
var authorizationStatus: CLAuthorizationStatus?
var currentLocation: CLLocationCoordinate2D?
override init() {
super.init()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
}
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
switch manager.authorizationStatus {
case .authorizedWhenInUse:
authorizationStatus = .authorizedWhenInUse
locationManager.requestLocation()
break
case .restricted:
authorizationStatus = .restricted
break
case .denied:
authorizationStatus = .denied
break
case .notDetermined:
print("location Manager: notDetermined")
authorizationStatus = .notDetermined
manager.requestAlwaysAuthorization()
break
default:
break
}
print("location Manager: \(locationManager.authorizationStatus)")
}
func requestLocationPermission() {
print("location Manager: \(locationManager.authorizationStatus)")
locationManager.requestAlwaysAuthorization()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let newLocation = locations.first else { return }
self.currentLocation = newLocation.coordinate
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("error: \(error.localizedDescription)")
}
func stopLocation() {
locationManager.stopUpdatingLocation()
}
}
VStack {
switch appStateManager.locationManager.authorizationStatus {
case .authorizedWhenInUse:
Text("Your current location is:")
Text("Latitude: \(appStateManager.locationManager.currentLocation?.latitude.description ?? "Error loading")")
Text("Longitude: \(appStateManager.locationManager.currentLocation?.longitude.description ?? "Error loading")")
case .restricted, .denied:
Text("Current location data was restricted or denied.")
Button("Open Settings") {
if let appSettings = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(appSettings, options: [:], completionHandler: nil)
}
}
case .notDetermined:
Text("Finding your location...")
Button("Request Location Permission") {
appStateManager.locationManager.requestLocationPermission()
}
Button("Open Settings") {
if let appSettings = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(appSettings, options: [:], completionHandler: nil)
}
}
default:
ProgressView()
}
}
@main
struct SupartApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
@State private var appStateManager = AppStateManager()
var body: some Scene {
WindowGroup {
RootView()
.environment(appStateManager)
}
}
}
@Observable
class AppStateManager {
var popupManager = PopupManager()
var locationManager = LocationManager()
}
CoreLocation returns .notDetermined on Simulator even though Location Services are enabled, how can I manually trigger the authorization dialog?
In my Info.plist, I have added the necessary keys for location permissions.
eg: details about the questions ..........................................................................
import Foundation
import CoreLocation
@Observable
final class LocationManager: NSObject, CLLocationManagerDelegate {
var locationManager = CLLocationManager()
var authorizationStatus: CLAuthorizationStatus?
var currentLocation: CLLocationCoordinate2D?
override init() {
super.init()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
}
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
switch manager.authorizationStatus {
case .authorizedWhenInUse:
authorizationStatus = .authorizedWhenInUse
locationManager.requestLocation()
break
case .restricted:
authorizationStatus = .restricted
break
case .denied:
authorizationStatus = .denied
break
case .notDetermined:
print("location Manager: notDetermined")
authorizationStatus = .notDetermined
manager.requestAlwaysAuthorization()
break
default:
break
}
print("location Manager: \(locationManager.authorizationStatus)")
}
func requestLocationPermission() {
print("location Manager: \(locationManager.authorizationStatus)")
locationManager.requestAlwaysAuthorization()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let newLocation = locations.first else { return }
self.currentLocation = newLocation.coordinate
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("error: \(error.localizedDescription)")
}
func stopLocation() {
locationManager.stopUpdatingLocation()
}
}
VStack {
switch appStateManager.locationManager.authorizationStatus {
case .authorizedWhenInUse:
Text("Your current location is:")
Text("Latitude: \(appStateManager.locationManager.currentLocation?.latitude.description ?? "Error loading")")
Text("Longitude: \(appStateManager.locationManager.currentLocation?.longitude.description ?? "Error loading")")
case .restricted, .denied:
Text("Current location data was restricted or denied.")
Button("Open Settings") {
if let appSettings = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(appSettings, options: [:], completionHandler: nil)
}
}
case .notDetermined:
Text("Finding your location...")
Button("Request Location Permission") {
appStateManager.locationManager.requestLocationPermission()
}
Button("Open Settings") {
if let appSettings = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(appSettings, options: [:], completionHandler: nil)
}
}
default:
ProgressView()
}
}
@main
struct SupartApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
@State private var appStateManager = AppStateManager()
var body: some Scene {
WindowGroup {
RootView()
.environment(appStateManager)
}
}
}
@Observable
class AppStateManager {
var popupManager = PopupManager()
var locationManager = LocationManager()
}
Share
Improve this question
edited Feb 21 at 7:59
Ehz uiq
asked Feb 21 at 5:41
Ehz uiqEhz uiq
515 bronze badges
4
|
1 Answer
Reset to default 0You have to request when in use before always
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
switch manager.authorizationStatus {
case .authorizedWhenInUse:
authorizationStatus = .authorizedWhenInUse
manager.requestAlwaysAuthorization()
locationManager.requestLocation()
break
case .restricted:
authorizationStatus = .restricted
break
case .denied:
authorizationStatus = .denied
break
case .notDetermined:
print("location Manager: notDetermined")
authorizationStatus = .notDetermined
manager.requestWhenInUseAuthorization()
break
default:
break
}
print("location Manager: \(locationManager.authorizationStatus)")
}
Also init
is too early so request authorization it us better to use onAppear
or task
, sometimes you need to add a small delay.
manager.requestWhenInUseAuthorization()
本文标签: swiftCoreLocation notDetermined how to trigger location permission request manuallyStack Overflow
版权声明:本文标题:swift - CoreLocation .notDetermined how to trigger location permission request manually? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745166144a2645691.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
appStateManager
is, and how you pass it to the view – workingdog support Ukraine Commented Feb 21 at 6:57