Disabling allowsBackgroundLocationUpdates (CLLocationManager) doesn't work after is was enabled

允我心安 提交于 2019-12-05 11:55:16

When the value of this property is false, apps receive location updates normally while running in either the foreground or background based on its current authorization. Updates stop only when the app is suspended, thereby preventing the app from being woken up to handle those events.Apple API Discussion.

I think the property discussion can solve your problem.

Despite it's maybe misleading name, it looks like requestWhenInUseAuthorization() gives you the behaviour you want (like @zhongwuzw said).

From the documentation:

If the user grants “when-in-use” authorization to your app, your app can start most (but not all) location services while it is in the foreground. (Apps cannot use any services that automatically relaunch the app, such as region monitoring or the significant location change service.) When started in the foreground, services continue to run in the background if your app has enabled background location updates in the Capabilities tab of your Xcode project. Attempts to start location services while your app is running in the background will fail.

If you need the "always" permission (or the user grants it without you asking for it through the Settings app), I found that stopping and immediately re-starting the location manager forces allowsBackgroundLocationUpdates to take effect.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!