问题
I am trying to implement location updates in the background based on a timer. I know all about the different location updating methods in iOS and have tried them but they don't fit the need. I need to check locations every so often even while not active. I've tried using timers but they don't execute in the background. That led me to find this question How do I get a background location update every n minutes in my iOS application? and it mentions using
UIApplication:beginBackgroundTaskWithExpirationHandler:
My questions is how do you use this method? I've searched the apple docs but they are recursively pointing at each other when dealing with background execution and location updates with no examples.
I have this code now based on some examples I've found:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
locationUpdater=[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:locationUpdater];
locationUpdater=UIBackgroundTaskInvalid;
} ];
}
And I have added the key "location" in the .plist for supported background modes
Does adding this just guarantee that my application won't be killed for some certain amount of time so I can keep a timer running? or where do I add the methods that are to get executed while in the background mode.
回答1:
If your background task runs longer than 10 minutes, your app will be terminated.
You really should use the os location services instead to get notified about location changes. Why is that not enough? If location doesn't change, what's the point of retrieving it over and over again, and once it changes, you'll be notified.
回答2:
For conceptual understanding have a look here.
For a detailed analysis of the accuracy you can visit this
you should start using startMonitoringSignificantLocationChanges
. You can find the sample code reference here
来源:https://stackoverflow.com/questions/7367644/background-tasks-iphone