Post

Replies

Boosts

Views

Activity

How can I get something similar to launchOptions[UIApplication.LaunchOptionsKey.location] in SceneDelegate for iOS 13+ apps
I'm making an app for iOS 13+ devices. My project has both app delegate and scene delegate files. I'm using core location for significant location changes. I was able to detect significant location changes and system waking up my app in simulator using SignificantlyChanged app’s scheme. But when the launch options dictionary has 0 elements and the value for the key UIApplication.LaunchOptionsKey.location is always nil. Here is my AppDelegate.swift where the system will launch the app in background state when a significant location change is detected and execute the location fetch code. And location manager class. I want to know if the app is launched my the system or if it is launched by the user so I want to check if UIApplication.LaunchOptionsKey.location key has a value. But it is always nil and there is no alternative to it in SceneDelegate. When and how can I find a solution to this long due issue. import CoreLocation @main class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. if launchOptions != nil{ if launchOptions![UIApplication.LaunchOptionsKey.location] != nil{ print("Called from background location fetch") } } LocationService.shared.askForPermission() LocationService.shared.delegate = self return true } // MARK: UISceneSession Lifecycle func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { // Called when a new scene session is being created. // Use this method to select a configuration to create the new scene with. return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) } func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) { // Called when the user discards a scene session. // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. // Use this method to release any resources that were specific to the discarded scenes, as they will not return. } } extension AppDelegate: LocationMangerDelegate{ func didFetchLocation(coordinate: CLLocationCoordinate2D) { print(coordinate) } func didFailToFetchLocation(error: Error?, message: String) { print(message) } }``` import Foundation import CoreLocation protocol LocationMangerDelegate: AnyObject{ func didFetchLocation(coordinate: CLLocationCoordinate2D) func didFailToFetchLocation(error: Error?,message: String) } class LocationService: NSObject{ private override init() { } weak var delegate: LocationMangerDelegate? static var shared = LocationService() lazy var locationManager: CLLocationManager = { var manager = CLLocationManager() manager.desiredAccuracy = kCLLocationAccuracyBest manager.allowsBackgroundLocationUpdates = true manager.pausesLocationUpdatesAutomatically = false manager.delegate = self return manager }() func askForPermission(){ print("INSIDE") locationManager.requestWhenInUseAuthorization() } func startLocationUpdates(){ self.locationManager.startUpdatingLocation() } func stopLocationUpdates(){ self.locationManager.stopUpdatingLocation() } } extension LocationService: CLLocationManagerDelegate{ func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) { let status = manager.authorizationStatus switch status { case .notDetermined: self.askForPermission() case .restricted: self.delegate?.didFailToFetchLocation(error: nil, message: "Location permission is restricted by the user") case .denied: self.delegate?.didFailToFetchLocation(error: nil, message: "Location permission is denied by the user") case .authorizedWhenInUse: self.locationManager.requestAlwaysAuthorization() case .authorizedAlways: self.locationManager.startMonitoringSignificantLocationChanges() @unknown default: break } } func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { if let location = locations.first{ print(location) self.delegate?.didFetchLocation(coordinate: location.coordinate) self.stopLocationUpdates() } } func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { print(error) self.delegate?.didFailToFetchLocation(error: error, message: error.localizedDescription) } }
1
0
377
Aug ’24