I wanted to cancel the method, to request a confirmation from the user when he presses the button "HOME" where on iPhone would go into the execution in background.
If the user accepts, enters into the execution in background, if not accept, I do not do anything.
I looked in the FORUM, in the documentation from Apple and I found nothing.
Could anyone help me?
Thanks in advance!
No you can't override this. The event will always fire but it does give you time to clean up before you move to the background.
Your delegate’s applicationDidEnterBackground: method has approximately five 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 application is terminated 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 five seconds
iOS Application Programming Guide
No, you can't override the operating system or the user's button press.
If the user presses the button to send the app into the background, the app goes into the background.
Related
I am creating an app where it will send you a local notification every 20 minutes. The problem I am having is once the app goes into background mode, the timer which triggers the function to run which executes some code then sends the user a notification won't run. Is there any alternative way of doing this? This timer runs on a loop, reseting its self every 20 minutes and running a function.
Thanks
Sure. Check out the documentation for UILocalNotification. You can set a fireDate to the exact second when you want the notification to go off. It also has parameters you can use to specify a repeat interval.
You cannot force the user to activate the app to run your function. Once the user taps the notification and your app becomes active, you can then perform all the logic you require (perhaps also including dealing with past "missed" notifications) .
The for loop is running fetching the data(images and text) from array but when i press home screen button the loop still continues in background and lasts more than 5 seconds which is default time, it should not be more that that as i studied while googling. And when i press home button even then the app delegates respective method like appEnterBackground also called after completion of this loop containing method. So, is it possible to break the loop when the home button is pressed.
Please guide.
If you wanted to detect the entering into background, in addition to responding to the app delegate applicationDidEnterBackground method, you could alternatively register yourself as an observer of the UIApplicationDidEnterBackgroundNotification notification (e.g., using the addObserverForName method of [NSNotificationCenter defaultCenter]).
Note, if you want to cancel your requests, you will want to ensure that (a) the requests are run asynchronously; and (b) they're cancelable. If you're targeting iOS 7 and later, you can accomplish this with NSURLSession (e.g., dataTaskWithRequest or dataTaskWithURL which return a NSURLSessionDataTask object, on which you can call the cancel method if and when desired). Then you can write a handler for the notification that cancels any pending requests.
If you really want to cancel the requests, you can do something like the above, but you alternatively could simply request additional time to complete the requests if the app happens to go into background while the requests are running. This way you get a few minutes to finish requests rather than just a few seconds. See Executing Finite-Length Tasks in App Programming Guide for iOS: Background States.
Or, perhaps even better, you could add your tasks to a background NSURLSession. See the Downloading Content in the Background section of the aforementioned App Programming Guide for iOS: Background States. This way the tasks will continue even after your app is suspended (or, if you app is terminated due to memory pressure). For more information, see WWDC 2013 video, What's New in Foundation Networking.
I have an app in Xcode which handles sensitive data. I would like to terminate the app / exit forcefully if there is inactivity for 5 mins. Can someone please tell me how to do this?
Thank you!
Click here for a tutorial on how to make a timer. Every action that the user takes, reset the timer. When the 5 minutes are up, you can use exit(0).
However, this method of programatically closing your app is discouraged by Apple, so use it at your own discretion.
Edit: In order to stop the timer, you need to store a pointer to the timer that you create, and then call:
[pointerToTimer invalidate];
pointerToTimer = nil;
Edit 2: An alternative to using exit(0) would be to make a almost blank screen except for some text that states:
You have been inactive for too long. Please exit and restart this application.
Make this screen appear once the timer gets to 5 minutes. Therefore, the user can't do anything on the app but look at the screen, or exit the app.
If you're writing an app to submit to the app store, you can't (according to the guidelines). See details in this note: http://developer.apple.com/library/ios/ipad/#qa/qa1561/_index.html
If you don't care about the store or interface guidelines, it suggests that exit() is available.
I was under the impression that NSTimer did not work at all after an application calls applicationWillResignActive. I seems however that existing NSTimers (i.e. ones created before the application resigned active) will continue to run and its only new NSTimers that can't be scheduled in this state, can anyone confirm this?
I am also assuming that its good (and Apple seems to say this too) that when your application calls applicationWillResignActive you should disable any NSTimers and start them again when applicationDidBecomeActive is called, does that make sense?
When an application is inactive, but still in the foreground (such as when the user gets a push notification or presses the sleep button) your application is still running completely. Any timers you have created which you don't stop will fire as normal. However, when your application goes to the background, if you are not registered to run a background thread all execution is stopped. If it is time for a timer to fire, it will not happen because the run loop is not running. When your application is reopened, however, any timers which were supposed to fire while it was in the background will all be fired immediately. Apple suggests doing cleanup in applicationWillResignActive so that you are not doing a lot of work when the user is not focused on your application, but you definitely want to disable timers before going to the background so that they don't all fire one after the other when your application is reopened.
Am I missing something here with Multitasking feature. I know we can execute a task in background, but what I really need is that task continues execution when app goes in background. So what I need is different than starting a new task in background. If images are being loaded in a view and user presses the home button to make the app go in background, I want that images continue to load and when user makes the app active again, he can see all the loaded images. How can this be done? Thanks a lot.
According to mentioned documentation, if you use beginBackgroundTaskWithExpirationHandler: while in foreground, it will keep executing in background until it ends. Once that task has ended, your application is eligible to be suspended.