Do simple task in the background after a long time on iOS 4+
Let me rephrase to make it clearer:
How can I have an "alarm" go off 20 minutes after my app has gone in the background?
Old question:
How can you have the simplest tast happen after 5-10-60 minutes on iOS? Es开发者_运维知识库sentially, have an NSTimer fire after a predefined interval.
I do "begin task", sleep( for some time ); and then "end task". However my app usually gets killed if I sleep for more than 5 minutes. I've seen posts around that mention that apps can stay alive in the background for a few hours, how exactly can that be achieved?
When your app goes to the background, the system will try to recollect as many as resources as you can give it to him like Images, nibs, etc. You have a notification for this. (– applicationDidEnterBackground:
, etc)
After that, it will "sort" all the background living apps according the resources usage order. The app that uses more resources comes first.
Then when a new app comes to the foreground if the systems thinks it needs more resources than it currently has it will start killing background apps. using the above sorted list.
This means that the less resources you use in the background, the longer your app will live. BUT, there is no guarantee it will live long. (Everything depends on user usage). There is also no notification when it gets killed!, so you cannot assume your app will live for 60 min.
You probably want to read this Apple doc on how to do something in the background (with some time restrictions, off-course) and probably also UIApplicationDelegate class reference.
Hope it helps ;)
Just use a local notification
精彩评论