Search code examples
swiftgpslocationappdelegateios15

iOS 15 app not launching in killed state with location change


I have added the background modes for location in my app SS here

But still, the app does not launch with the launch options key

UIApplication.LaunchOptionsKey.location

Here is my app delegate code,

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    addItem(name: "LaunchOptions: \(String(describing: launchOptions))")
    if let launchOptions = launchOptions {
        if launchOptions[UIApplication.LaunchOptionsKey.location] != nil {
            startLocationMonitoring()
        }
    }
    print("LaunchOptions: \(String(describing: launchOptions))")
    return true
}

func startLocationMonitoring() {
    locationManager?.delegate = nil
    locationManager = nil
    locationManager = CLLocationManager()
    locationManager?.delegate = self
    locationManager?.desiredAccuracy = kCLLocationAccuracyBestForNavigation
    locationManager?.distanceFilter = 1.0 // meters
    locationManager?.allowsBackgroundLocationUpdates = true
    locationManager?.activityType = .otherNavigation
    locationManager?.pausesLocationUpdatesAutomatically = false
    authStatusCheck()
    locationManager?.startUpdatingLocation()
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    print(locations)
    if let appDelegate = (UIApplication.shared.delegate as? AppDelegate) {
        for location in locations {
            appDelegate.addItem(newLocation: location)
        }
    }
}

addItem function adds the location change or the logs to core data


Solution

  • now you get launch options on your scene delegate. Here is a quick example:

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {                
              if let response = connectionOptions.notificationResponse {
                 //get your launch info from here
                 let location = response.notification.request.content.userInfo[UIApplication.LaunchOptionsKey.location]
        ...
              }
            }