3

I have a button that will close my app.

I am using exit(0); but I dont like the way it closes my app, its like when an app crashes and the app closes kinda close. I am looking for away to terminate when the user pushes this button. Any suggestions?

Thanks

I have tried the following:

[[NSThread mainThread] exit];

but got this error:

No visible @interface for 'NSThread' declares the selector 'exit'
user979331
  • 11,039
  • 73
  • 223
  • 418

2 Answers2

2

The is no proper way to do that. You should use exit(0); to go from application. But for increasing of the user experience just add some few lines :

        //home button press programmatically
        UIApplication *app = [UIApplication sharedApplication];
        [app performSelector:@selector(suspend)];

        //wait 2 seconds while app is going background
        [NSThread sleepForTimeInterval:2.0];

        //exit app when app is in background
        exit(0);
Oleg Gordiichuk
  • 15,240
  • 7
  • 60
  • 100
2

The only approved method to exit an app it to set the (Apple documented) exits-on-suspend app plist key, and then have the app send a URL to launch another app (such as Safari). iOS will then cleanly terminate your app instead of backgrounding it.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
  • Didn't know this mechanism existed. Could you please provide a link of the related documentation – Vin Nov 29 '15 at 09:10
  • See: https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html – hotpaw2 Nov 29 '15 at 14:58
  • That's the ''Application does not run in background" key. Wouldn't using that mean we are never able to run our app in background even when force closing mechanism was required in one use case? – Vin Nov 30 '15 at 05:11