-3

In application that I work on it, I need to close it from code. I'm using exit(0) for that.

Yes, I know that i shouldn't close application from code

When I close application in this way, and run it again, there is some strange behaviur - application doesn't start nor from splash screen, like new instance of app, neither from last view, like application turning back from background.

Instead, for a second some random, non-interactive view of my app is appearing, and after this splash screen start to run.

I'm confused because I cannot find source, and how to fix this ugly bug. Can you help me?

Thaven
  • 1,857
  • 3
  • 19
  • 35
  • 2
    How to fix it? Don't exit from code. – Nick Bull Sep 11 '12 at 08:43
  • [here](http://stackoverflow.com/a/12195593/1603234)'s an answer, if you want to exit from Application! – Hemang Sep 11 '12 at 08:51
  • thanks you for answers, but as i wrote - i **know** that i shouldn't use exit(0) - telling me not to do this is not the answer i expect :) – Thaven Sep 11 '12 at 09:32
  • I'm just so curious to know why someone would like to exit the application while still knowing that one should not do that. Can you pleease tell us why? =) – jake_hetfield Sep 11 '12 at 09:33
  • @jake_hetfield - very short answer: by boss tell me to do this ;). and i haven't influence on his decision. – Thaven Sep 11 '12 at 09:48
  • 1
    @Thaven Just because that's not the answer you expect doesn't mean it isn't the correct answer. There are reasons why you are told NOT to use exit(0) and as you have discovered, the behaviour you are experiencing is one of the reasons why. Perhaps you should give your boss the relevant sections from Apple's development guides and then let him argue with them about it? – Nick Bull Sep 11 '12 at 10:01
  • @NickBull - I tried few days ago. As You can see - without effect. – Thaven Sep 11 '12 at 10:12
  • 2
    Show this question to your boss. Then quit your job and find a better boss. – jake_hetfield Sep 11 '12 at 11:38

3 Answers3

6

This is exactly the problem with exit(0). You get weird multitasking behaviour and all sorts of other problems.

You simply can't terminate your application in code. You should explain to us why you think you need to quit programmatically so we can help you find an alternative solution.

Mike Weller
  • 45,401
  • 15
  • 131
  • 151
  • 1
    this is right, all of the UIApplicationDelegate lifecycle methods are skipped, so there could be lots of built-in functionality you are skipping (e.g. taking a screenshot of the current app to display when you come back from background, which sounds similar to what you are seeing) – wattson12 Sep 11 '12 at 08:44
  • @wattson12 - thanks for answer - You are the only that actually answer my question. Can you copy you comment as answer, so i can accept it? If I had any influence on the decisions, I would not do any application terminating... – Thaven Sep 11 '12 at 09:34
  • @Thaven I think Parags answer says the same thing really, its basically the same response as mine, and has the link I was going from – wattson12 Sep 11 '12 at 09:36
5

From Technical Q&A QA1561
There is no API provided for gracefully terminating an iOS application.

Do not call the exit function. Applications calling exit will appear to the user to have crashed, rather than performing a graceful termination and animating back to the Home screen.
Additionally, data may not be saved, because -applicationWillTerminate: and similar UIApplicationDelegate methods will not be invoked if you call exit. If during development or testing it is necessary to terminate your application, the abort function, or assert macro is recommended.

Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
0

Instead of

exit(0) 

try this:

[[UIApplication sharedApplication] performSelector:@selector(terminateWithSuccess)];
Stefan
  • 518
  • 2
  • 6