I want to get new location every 1min when my app is in background or killed. Here is my code :
Location Service:
class LocationService: NSObject, CLLocationManagerDelegate {
static let shared = LocationService()
var locationManager = CLLocationManager()
var significatLocationManager = CLLocationManager()
var currentLocation: CLLocation!
var timer = Timer()
var bgTask = UIBackgroundTaskInvalid
func startUpdatingLocation() {
locationManager.requestAlwaysAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = kCLDistanceFilterNone
if #available(iOS 9.0, *) {
locationManager.allowsBackgroundLocationUpdates = true
}
locationManager.delegate = self
locationManager.startUpdatingLocation()
}
func stopUpdatingLocation() {
locationManager.stopUpdatingLocation()
timer.invalidate()
}
func restartUpdatingLocation() {
locationManager.stopMonitoringSignificantLocationChanges()
timer.invalidate()
startUpdatingLocation()
}
func startMonitoringLocation() {
locationManager.stopUpdatingLocation()
locationManager.startMonitoringSignificantLocationChanges()
timer.invalidate()
}
func changeLocationAccuracy(){
switch locationManager.desiredAccuracy {
case kCLLocationAccuracyBest:
locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers
locationManager.distanceFilter = 99999
case kCLLocationAccuracyThreeKilometers:
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = kCLDistanceFilterNone
default: break
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
bgTask = UIApplication.shared.beginBackgroundTask(expirationHandler: {
UIApplication.shared.endBackgroundTask(self.bgTask)
self.bgTask = UIBackgroundTaskInvalid
})
currentLocation = locations.last!
let interval = currentLocation.timestamp.timeIntervalSinceNow
let accuracy = self.locationManager.desiredAccuracy
if abs(interval) < 60 && accuracy != kCLLocationAccuracyThreeKilometers {
timer = Timer.scheduledTimer(timeInterval: 60, target: self, selector: #selector(LocationService.changeLocationAccuracy), userInfo: nil, repeats: false)
changeLocationAccuracy()
}
}
}
App delegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if launchOptions?[UIApplicationLaunchOptionsKey.location] != nil {
LocationService.shared.restartUpdatingLocation()
}
}
func applicationDidEnterBackground(_ application: UIApplication) {
LocationService.shared.restartUpdatingLocation()
}
func applicationWillTerminate(_ application: UIApplication) {
LocationService.shared.startMonitoringLocation()
}
View controller:
override func viewDidLoad() {
LocationService.shared.startUpdatingLocation()
}
But my code is not working, i'm not getting new location after a few minutes when my app is in background. and same when my app is suspended or killed. Is there any solution for that ? please not that i'm using Swift 3 & xcode 8.1.
Thank's in advance
backgroundFetch
. – Orren