Getting user location every n minutes after app goes to background
After some days trying all possible solutions I was finally able to make it work. Here is what was wrong in my solution:
- I need to setup 2 UIBackgroundTaskIdentifiers, one for the timer, and another one for the locationManager
On my solution, just add:
UIApplication *app = [UIApplication sharedApplication];
bgTask2 = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask2];
bgTask2 = UIBackgroundTaskInvalid; }];
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
Send the location to the server every n minutes in the background in swift
In DidBackground:
- stop normalTimer.
- start New backgroundTaskTimer
In WillForeground:
- stop backgroundTaskTimer.
- start normalTimer
Find below sample code:
In Appdelegate:
Declaration:
var backgroundUpdateTask: UIBackgroundTaskIdentifier!
var backgroundTaskTimer:Timer! = Timer()
Implementation:
func doBackgroundTask() {
DispatchQueue.global(qos: .default).async {
self.beginBackgroundTask()
if self.backgroundTaskTimer != nil {
self.backgroundTaskTimer.invalidate()
self.backgroundTaskTimer = nil
}
//Making the app to run in background forever by calling the API
self.backgroundTaskTimer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(self.startTracking), userInfo: nil, repeats: true)
RunLoop.current.add(self.backgroundTaskTimer, forMode: RunLoopMode.defaultRunLoopMode)
RunLoop.current.run()
// End the background task.
self.endBackgroundTask()
}
}
func beginBackgroundTask() {
self.backgroundUpdateTask = UIApplication.shared.beginBackgroundTask(withName: "Track trip", expirationHandler: {
self.endBackgroundTask()
})
}
func endBackgroundTask() {
UIApplication.shared.endBackgroundTask(self.backgroundUpdateTask)
self.backgroundUpdateTask = UIBackgroundTaskInvalid
}
In ApplicationDidEnterBackground:
func applicationDidEnterBackground(_ application: UIApplication) {
//Start the background fetch
self.doBackgroundTask()
}
In ApplicationWillEnterForeground:
func applicationWillEnterForeground(_ application: UIApplication) {
if self.backgroundTaskTimer != nil {
self.backgroundTaskTimer.invalidate()
self.backgroundTaskTimer = nil
}
}
Background service in android 10 to get location in every 10 minutes
Due to the android's limitations for background tasks in post-O devices, background services are destroyed by the android system if the app is in the background for some time.
You should use startForeground()
in onStartCommand()
to start the service as a foreground service and also show the notification for the same.
How do I get a background location update every n minutes in my iOS application?
I found a solution to implement this with the help of the Apple Developer Forums:
- Specify
location background mode
- Create an
NSTimer
in the background withUIApplication:beginBackgroundTaskWithExpirationHandler:
- When
n
is smaller thanUIApplication:backgroundTimeRemaining
it will work just fine. Whenn
is larger, thelocation manager
should be enabled (and disabled) again before there is no time remaining to avoid the background task being killed.
This works because location is one of the three allowed types of background execution.
Note: I lost some time by testing this in the simulator where it doesn't work. However, it works fine on my phone.
Related Topics
No Avplayer Delegate? How to Track When Song Finished Playing? Objective C iPhone Development
How to Create Managedobjectcontext Using Swift 3 in Xcode 8
How to Use Avcapturephotooutput
Disable Uipageviewcontroller Bounce
Get Progress from Datataskwithurl in Swift
Apns (Apple Push Notification Service) Reliability
How to Create an Umbrella Framework in iOS Sdk
How to Detect When Keyboard Is Shown and Hidden
iPhone Sdk Cgaffinetransform Getting the Angle of Rotation of an Object
Round Top Corners of a Uibutton in Swift
How to Reset the Privacy Settings in iOS
How to Make a Background Image Scale to Screen Size in Swift
How to Get Unique Id in iOS Device
Is It Considered a Private API to Use App-Prefs:Root
Swift Editor Placeholder in Source File
In Swift, How to Avoid Both Optionals and Nil Object References