Hope i understood your question,
First of all you need get the user's permission, call the requestAlwaysAuthorization() method
Code Sample from Apple:
let locationManager = CLLocationManager()
func enableLocationServices() {
locationManager.delegate = self
switch CLLocationManager.authorizationStatus() {
case .notDetermined:
// Request when-in-use authorization initially
locationManager.requestAlwaysAuthorization()
break
case .restricted, .denied:
// Disable location features
disableMyLocationBasedFeatures()
break
case .authorizedAlways:
// Enable any of your app's location features
enableMyAlwaysFeatures()
break
}
}
}
Note:- You are required to include the NSLocationWhenInUseUsageDescription and NSLocationAlwaysAndWhenInUseUsageDescription keys in your app's Info.plist file. (If your app supports iOS 10 and earlier, the NSLocationAlwaysUsageDescription key is also required.) If those keys are not present, authorization requests fail immediately.
Verify these steps:
1.set location manager's allowsBackgroundLocationUpdates to YES.
2.call the requestAlwaysAuthorization() method
3.Add NSLocationAlwaysUsageDescription in info.plist.
4.configure the app to run in the background(UIBackgroundModes in plist) maybe in sandbox.