0

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.

Surjeet Singh
  • 11,691
  • 2
  • 37
  • 54
  • You cannot keep the app active in the background. iOS gives your app a short amount of time to run code before it suspends it. Your app can respond to the location updates only and should only need a second or so per update – Scriptable Nov 15 '18 at 09:44
  • With UIBackgroundTask, I am able to run app for about 3m minutes. I want to run app more than that if required. – Surjeet Singh Nov 15 '18 at 09:50
  • yes I understand your requirement. It has been asked on here many times, but iOS does not allow you to do it – Scriptable Nov 15 '18 at 09:52
  • you can check app for background fetch but you need to provide valid reason once you want to publish it to the appstore – Stefan Nov 15 '18 at 09:54
  • 180 seconds is the background execution limit. – Paulw11 Nov 15 '18 at 09:58
  • @Paulw11 Is it mentioned somewhere in apple docs Or it is based on personal experience ? – Surjeet Singh Nov 15 '18 at 10:05
  • It is mentioned somewhere. – Paulw11 Nov 15 '18 at 10:06
  • See here: https://stackoverflow.com/questions/28275415/how-long-does-apple-permit-a-background-task-to-run – Paulw11 Nov 15 '18 at 10:07
  • @Paulw11 Do you have that link, If yes. please share, then I can convince client for alternative. – Surjeet Singh Nov 15 '18 at 10:07
  • Test it with: "Note: Always provide an expiration handler when starting a task, but if you want to know how much time your app has left to run, get the value of the backgroundTimeRemaining property of UIApplication." https://developer.apple.com/library/archive/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html#//apple_ref/doc/uid/TP40007072-CH4-SW4 – Scriptable Nov 15 '18 at 10:25
  • @Scriptable I have expiration handler block when starting task, and also checked backgroundTimeRemaining property with a timer. It return me 3 min to execute, and terminated after that. – Surjeet Singh Nov 15 '18 at 10:27
  • So there you go... you have 3 minutes. that is your answer. you can't get any more . explain that to your client – Scriptable Nov 15 '18 at 10:29
  • But I need to extend this time, if required. Which I could not achieve right nw. – Surjeet Singh Nov 15 '18 at 10:42
  • https://medium.com/@calvinlin_96474/ios-11-continuous-background-location-update-by-swift-4-12ce3ac603e3 – canister_exister Nov 15 '18 at 15:12

1 Answers1

0

There is already an official way to do this: Just use Always-Authorized. With it you can check the userlocation in a special period.

Jonathan
  • 139
  • 1