Background tasks iPhone

北城以北 提交于 2020-01-02 05:31:05

问题


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

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