Terminate xcode app due to time out - iphone

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.

Related

Is there a method if application was closed in multitasking?

Is there a method like: wasTerminated or something like that? I want to display the user an alertsheet, if he has completely closed the app from multitasking. At the moment I have implemented my code in the DidEnterBackround, but is there another way to fix this? That would be very useful for me.
Sorry I'm a newbie and I didn't find a solution on the net.
Thanks.
Background applications can be terminated at any time, and there's no way for an app to tell whether it was terminated by the user (through the multitasking switcher) or by the system.
Your only notifications are –applicationDidEnterBackground:, –applicationWillEnterForeground:, and –application:didFinishLaunchingWithOptions:. With these notifications you can tell if some background task completed or not, but you can't tell how your app was terminated.
Do you want to show the message to the user, next time the user starts your app, or when the app is killed. If the app is killed by double clicking on the home button and killing it, and if you want to find out then, it is not possible.
But if the app is killed and the user starts the app again, then you will know whether the app is started fresh or did it become active.
The method
-(void)applicationWillEnterForeground:(UIApplication *)application{
NSLog(#"Entering foreground");
}
will get called if it becomes active from background.
Other wise,
- (void)applicationDidBecomeActive:(UIApplication *)application {
}
will get called where you can put your alert view.
You can easily experiment with these situations and figure out a way to do what you want.

objective-c background process application

I am doing the iOS timer application, but when I press the home button the timer will stop.
I would like to know if close the application, the program can keep working?
And go back the application can keep running.
I see some article How to write a background service in iphone application? said can't run in background.
Can some one help me?
Thank you.
If it's a timer you are doing, you can always keep the time when it started, through CACurrentMediaTime(), and if your application goes in the background, when it comes to the foreground again, in applicationDidBecomeActive:(UIApplication *)application, take another CACurrentMediaTime() poll, and display the difference between the numbers.
I believe this is how the Apple's bundled app works. No need to keep processing that number if the application is not used.

Objects not being released fast enough, causing an app relaunch crash

I have an app where I have 5 sets of animations that I'm storing in an array. The animations get picked to play randomly after a button touch. This is all working perfectly, however I noticed a bug when I quit the app and reopen immediately, I'll see my main view, then it'll jump to my second view that has the animation in it. (This shouldn't happen since you have to tap the main view in order for it to modally swap in the second view. If I interact with it everything works for a few seconds, then it closes with no crash log.
I finally realized that some of the objects must not be getting released fast enough, since if I close the app and wait three seconds, then reopen, everything executes fine.
I didn't want to put down code to show as this is more of a brainstorming question. I'd love any insight that could point me the right way. I changed a lot of my code to get rid of convenience methods and have all my variables defined and then released in my dealloc.
Is there a way to truly tell the app to kill everything on quit? It's not set to run in the background so this is a bit odd. Thanks for your help I'm still new to this and learning!
Alright, after working on this all weekend and doing more research comparing a barebones version of my app to my prerelease version, I traced memory leaks to the Flurry Analytics api that I am using. Apparently I was suffering from the same issue as the post here: App hangs on restart with latest Flurry SDK and ios4 . I resolved this by setting these optional methods to false, since they take extra time to send data after the app terminates, and depending on the connection it takes a few seconds.
FlurryAnalytics.h
/*
optional session settings that can be changed after start session
*/
+ (void)setSessionReportsOnCloseEnabled:(BOOL)sendSessionReportsOnClose; // default is YES
+ (void)setSessionReportsOnPauseEnabled:(BOOL)setSessionReportsOnPauseEnabled; // default is YES
Hope this helps anyone else who experienced something similar to me!
All apps can enter the background by default. Normally they do not do anything there, but they stay there in a frozen state and when you open them again, your program does not restart, it just picks up where it left off.
Anything that's set as an animation delegate might not get released, since it's retained for that purpose until the animation completes.
You can add an applicationDidEnterBackground: method to your app delegate to get informed when your app is going into the background, but exactly what you need to do depends on the design of your app. You can also add applicationWillEnterForeground: to do anything you need to do differently when restarting, as opposed to newly starting.
You might be able to force your animations to complete by starting a new animation with duration 0.0 (or very short if for some reason you can't do that).
If this happens only if your app goes to bkgnd and comes back AND you don't mind if the app restarts everytime it comes back then just put UIApplicationExitsOnSuspend in your app's plist. In all my cases where these and other bad things happen with apps going to and returning from bkgnd this helped.
While you might still see the app on the buttom when double tapping it is really stopped and will restart. Apps that show on the buttom do not always have to run or be stored in the bkgnd I learned.
ps. don't forget to set the value of UIApplicationExitsOnSuspend to YES

(iPhone iOS 4) App that was closed and removed from background bar

Consider an app that was in the multitask bar, and was closed, clicking on the minus sign.
What is supposed to be the behavior of this app at restart?
Does the app restart from scratch with the splash screen and without old data?
Or does the app load saved data and restart without splash screen and with previous data reloaded?
Thank you.
The application should restore its previous state in as many cases as possible. Prior to multitasking, all the applications tried to do this, to give the users the impression of multitasking. With multitasking in iOS 4, this doesn't change. You still need to do this to:
Support older devices.
Keep the user experience the same even if the user opens tons of apps and the device frees memory automatically.
If its closed then it will start from "scratch" unless you use the app delegate methods which are fired to store data and create the functionality to reload somewhere specific in your app yourself.
The splash screen will show if there is loading time needed, just like when you start an app the first time.
I think a user who clicks the minus is probably doing it, either just to clean up, or because he/she would like the app to return to it's beginning setup, just as if the device was reset.
I handle the minus, just like that, and start fresh with the splash screen. (not sure what the apple guide recommends)
This operation (tapping on minus icon) is intended for freeing memory.
When Apple has introduced multitasking in iOS, usage of memory has increased a lot!
So, why you have to tap on minus to kill application? When do you have to do this?
When your device responsiveness is too bad.
After killing (closing) application, when you restart the app, it start from scratch or resume last operation depending on how the application has developed!
For example, look at FacebookApp. If you are logged-in and quit app, at next restart it's reopen your account and last page seen!
For something like a game, I would suggest you restore the user's state ONLY IF they were in the middle of a game, else just start from scratch. However, for anything else, always start from scratch.
If you do nothing, your app will probably start from scratch at the moment.

Crash on resuming app within 11 seconds after exit(0)

I have an app that imports data on start using Core Data. In this part of the app, I have disabled resuming (multitasking) by calling exit(0) in applicationDidEnterBackground when a flag is set. E.g.:
-(void)applicationDidEnterBackground:(UIApplication *)application {
if (allowResuming==NO){
NSLog(#"Terminating...");
exit(0);
}
}
If I then attempt to 'resume' the app (by pressing its icon) within 11 seconds of having pressed the Home button (and, therefore, exit(0) having been called), the app crashes. This doesn't happen when running through the debugger, but the crash logs suggest that it looks like the app is trying to resume the data import where it left off, which, of course, is not what I want.
Attempting to 'resume' the app again straight after this crash (i.e. within a second) is successful.
If I attempt to 'resume' the app after 11 seconds, it's fine.
I would be really grateful if anyone has any ideas and/or can point me in the right direction here.
I don't think you should be doing exit(0). To disable Multitasking, set the key UIApplicationExitsOnSuspend key in Info.plist to YES. For details, see the section Opting Out of Background Execution at
http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/CoreApplication/CoreApplication.html#//apple_ref/doc/uid/TP40007072-CH3-SW1.
To discard the partial work done when the app leaves foreground, add a listener for UIApplicationWillResignActiveNotification or UIApplicationDidEnterBackgroundNotification. In the handler method, you could do something like-
[self discardPartialWorkDone];
HTH,
Akshay
You should not use exit(0) like Akshay said. In addition to that Apple will probably reject your App, because Apps which consist of code which interrupt the App like this wont be approved.