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 with UIApplication:beginBackgroundTaskWithExpirationHandler:
- When
n
is smaller than UIApplication:backgroundTimeRemaining
it will work just fine. When n
is larger, the location 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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…