Search code examples
objective-cnstimerfreezeterminatequit

quit the application by setting NSTimer?


is their any better way to quit the application programmatically?

recently i made a radio application that have a user setup to set a time using NSTimer for quit the app process ( i mean sleep time). when the time reaches the app should be stop its process and quit.

i used these statements to quit the app when time reaches,

  [[UIApplication sharedApplication] suspend];
  [[UIApplication sharedApplication] terminateWithSuccess];

theTimer=[NSTimer scheduledTimerWithTimeInterval:(1.0/1.0) target:self selector:@selector(countTime) userInfo:nil repeats:YES];

counter-=1;
timeLeft.text=[NSString stringWithFormat:@" %d",counter];
if (counter==0.0) {

    [theTimer invalidate];
  [[UIApplication sharedApplication] suspend];
  [[UIApplication sharedApplication] terminateWithSuccess];

is their any problem by using [[UIApplication sharedApplication] suspend]; [[UIApplication sharedApplication] terminateWithSuccess]; methods

any other better way to quit the app, or at least freeze the app process.. i need help?


Solution

  • I've always used exit(0). I suppose that if you have to run code before the app quitting you should call it before exit(0).

    Apple discourages the use of exit() because from the user point of view it looks like a crash. But they also say there is no API call to gracefully terminate your app.

    If you're looking for a way to terminate your app without the user pressing home, during sleep time, I assume he won't confuse it with a crash, since he won't be paying attention to your app and exit() leaves no crash log.

    So, your options are:

    • Forget this and just beep after some time to remind him to close the app. (terrible!)
    • Close the app using your private calls and risk Apple's rejection.
    • Close the app using exit() and stick with POSIX (accepted) calls.