简体   繁体   English

iOS:通过位置更新在后台运行应用

[英]iOS: Run app in background with location updates

I have the intention to develop an iOS app which works on geo-fencing enter/exit events. 我打算开发一个可处理地理围栏进入/退出事件的iOS应用。

I need to run my app while app is running in background or when invoked by OS for about 5 minutes. 当应用程序在后台运行或被OS调用约5分钟时,我需要运行我的应用程序。 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. 问题是应用程序只能运行约180秒,然后后台任务会自动终止。 I tried to re-initiate background task when finished, but it didn't worked and app execution stop after 3 min. 我尝试在完成后重新启动后台任务,但没有成功,并且3分钟后应用程序执行停止。

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 ? 是否可以长时间运行应用程序180秒以上? I am testing with iOS 11 and 12. 我正在使用iOS 11和12进行测试。

Edit: I tried recalling keepAlive function when background task complete, But second time background execution time doesn't increase and app terminated. 编辑:我尝试在后台任务完成时重新调用keepAlive函数,但是第二次后台执行时间没有增加并且应用终止了。

There is already an official way to do this: Just use Always-Authorized . 已经有一种官方的方法可以做到这一点:只需使用Always-Authorized即可 With it you can check the userlocation in a special period. 有了它,您可以在特殊时期检查用户位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM