I have the intention to develop an iOS app which works on geo-fencing enter/exit events.
I need to run my app while app is running in background or when invoked by OS for about 5 minutes. Like when receive notification for event monitoring.
I have enabled background execution with location update and whenever required, run background task to continue execution in background.
Problem is app run's only for about 180 seconds and after that background task terminated automatically. I tried to re-initiate background task when finished, but it didn't worked and app execution stop after 3 min.
func keepAlive() {
backgroundTask = UIApplication.shared.beginBackgroundTask { [weak self] in
UIApplication.shared.endBackgroundTask(self!.backgroundTask)
self?.backgroundTask = UIBackgroundTaskInvalid
self?.keepAlive()
}
}
Is it possible to run app for longer time more than 180 sec ? I am testing with iOS 11 and 12.
Edit:
I tried recalling keepAlive
function when background task complete, But second time background execution time doesn't increase and app terminated.
There is already an official way to do this: Just use Always-Authorized. With it you can check the userlocation in a special period.