I want to get location information even after the application ends.

Asked 2 years ago, Updated 2 years ago, 97 views

I would like to use CoreLocation to save the user's location information to UserDefault at the time of fore, background, and application termination on iphone ios 13 swift 5.2.
Fore, I think I can do the background, but I don't know how to get location information after finishing the app.

I checked the Background Models > Location Update.Is there anything else I need?
AuthorizationStatus is always allowed.
I'm at a loss because I couldn't find it when I searched for it.

Thank you for your cooperation.

swift ios iphone swift5

2022-09-30 11:25

2 Answers

Apple's official website includes the following documents:

Handling Location Events in the Background

As for the required settings, please check them carefully again (I don't see Info.plist settings on the same page, but I think you will find good articles in Japanese including iOS13 changes), you should see Table 1 (Table-1).

Among them, Launches app means that it will launch the app (stopped).Only the following three services are marked with Yes in the column:

  • Significant-change location service
  • Visits service
  • Region monitoring

I think the "location acquisition" you refer to is probably a standard location service, but you can see that the line Launches app is No.

That is, as long as you use the regular location service,

No matter what you set up, you will not be able to retrieve location information while the app is down.

(Visits service and Region monitoring are very far from normal location services) Rewrite Significant-change location service specifications to use them if they fit your app's purpose.

If you don't think you can use it, I think you need to review the functionality of the app on the premise that you can't do that.

"Also, it may be just that my method was bad, but when I tried the Significant-change location service a long time ago, I could only pick up the information with the accuracy of ""lucky if I could pick up the location information well.""Before applying it to a real app, it is recommended that you create a verification code to see if it is useful and try it on a real machine.


2022-09-30 11:25

Thank you.Handling Location Events in the Background was helpful.
The following contents are coded based on the information on the site.
Get launchOptions in willFinishLaunchWithOptions and use the
key to
When UIAapplication.LaunchOptionsKey was set, the application that was terminated by os was called, so I regenerated the object.Switch to Significant-change location service.
Normally, we call startUpdatingLocation and run it on standard.
When I tried it on the actual machine, the fore and background were fine, but I didn't really understand the condition of TERMINATED.There was no location information available because the distance traveled was hundreds of meters.
When switching to stCall startUpdatingLocation and run on standard, 切り替える I was not sure if I should recreate the locationManager or if the parameters are different for each method I call when I do.


@UIAapplicationMain
classAppDelegate:UIResponder,UIApplicationDelegate{
    func application(_application:UIApplication, willFinishLaunchingWithOptions launchOptions:[UIApplication.LaunchOptionsKey:Any]?) ->Bool{
        iflet_=launchOptions?[UIApplication.LaunchOptionsKey.location]{
            generateManager()
            locationManager?.stopUpdatingLocation()
            locationManager?startMonitoringSignificantLocationChanges()
            return true
        }
        generateManager()
        locationManager?startUpdatingLocation()
        locationManager?stopMonitoringSignificantLocationChanges()
        return true
    }

    func applicationWillEnterForeground(_application:UIAapplication){
        // Notify when the app moves to the foreground
        locationManager?startMonitoringSignificantLocationChanges()
        locationManager?startUpdatingLocation()

    }
    func applicationDidEnterBackground(_application:UIAapplication){
        locationManager?.stopUpdatingLocation()
        locationManager?startMonitoringSignificantLocationChanges()
    }
    

    func generateManager() {
        locationManager = CLLocationManager()
        locationManager?.allowsBackgroundLocationUpdates=true
        locationManager ?.pauseLocationUpdatesAutomatically=false
        locationManager?.desiredAccuracy=kCLLocationAccuracyBest
        locationManager?.distanceFilter=5
        locationManager ?.delegate=self
        locationManager?.requestAlwaysAuthorization()
    }

}

extensionAppDelegate:CLLocationManagerDelegate{
    
    US>funclocationManager(_manager:CLLocationManager, didFailWithError:Error) {
        print("error::\(error.localizedDescription)")
    }
    
    funclocationManager(_manager:CLLocationManager, didUpdateLocations:[CLLocation]){
        iflet latlon=locations.last {
            let latitude = latlon.coordinate.latitude
            let longitude=latlon.coordinate.longitude
            let model = LocationModel()
            model.lat=latitude
            model.lon=longitude
            // Save location information here
        }
    }
}


2022-09-30 11:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.