0

Is there a way to execute a piece of code after certain amount of time after I quit the application?

I mean press the home button

canhazbits
  • 1,664
  • 1
  • 14
  • 19
  • you might want revise your question – timpone Jan 17 '14 at 03:03
  • 1
    Excite the code? Well, I suppose you could use a feather to tickle the iDevice's ram chips and hope you hit the transistors where your code's being stored... – Marc B Jan 17 '14 at 03:05
  • 2
    What state is your application in when you want to execute your code? If you need help figuring this out, take a look at [App States and Multitasking](https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html) – Chelsea Kelly-Reif Jan 17 '14 at 03:06
  • See applicationWillEnterBackground and applicationDidEnterBackground – Horst Jan 17 '14 at 03:08
  • How much time is "a certain amount"? Seconds? Minutes? Hours? – Hot Licks Jan 17 '14 at 03:19
  • possible duplicate of [iPhone - Backgrounding to poll for events](http://stackoverflow.com/questions/4656214/iphone-backgrounding-to-poll-for-events) – nalyd88 Jan 17 '14 at 03:24

3 Answers3

1

https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIApplicationDelegate/applicationDidEnterBackground:

The applicationDidEnterBackground method is called on the appDelegate in iOS 4.0 and onwards, upon the home button being pressed and the user's view exiting to the home screen. You can then use a [NSTimer]

(https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/classes/NSTimer_Class/Reference/NSTimer.html)

to execute whatever code you want at some point later. Note, though, that iOS likes applicationDidEnterBackground to return within about 5 seconds; your application can start a background task with the method beginBackgroundTaskWithExpirationHandler.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
1

From the Apple docs:

Your app delegate’s applicationDidEnterBackground: method has approximately 5 seconds to finish any tasks and return. In practice, this method should return as quickly as possible. If the method does not return before time runs out, your app is killed and purged from memory. If you still need more time to perform tasks, call the beginBackgroundTaskWithExpirationHandler: method to request background execution time and then start any long-running tasks in a secondary thread. Regardless of whether you start any background tasks, the applicationDidEnterBackground: method must still exit within 5 seconds.

Read the whole article for more information but basically when someone presses the home button the applicationDidEnterBackground: method will get called. In that method setup your code on a separate thread using beginBackgroundTaskWithExpirationHandler:.

Hope that helps!

nalyd88
  • 4,940
  • 8
  • 35
  • 51
1

if You want to execute some code after some time than you can use the NSTimer.Thanks

[NSTimer scheduledTimerWithTimeInterval:60.0
                                                 target:self
                                               selector:@selector(MethodName)
                                               userInfo:nil
                                                repeats:YES];
Harunmughal
  • 367
  • 1
  • 4