The team is in the middle of building an iOS app for iPad using Xamarin which will be enterprise deployed.  A requirement came up to automatically shut down the after a certain action was performed by the user.  Usually it’s recommended to not ‘kill’ your app on iOS and apps may fail certification if you do this.  Make sure to read the iOS documentation.

Now because we are doing an Enterprise Deploy and not an App Store Deploy, we have a few options and won’t fail any certification for using these techniques, but if you are going through the App Store certification process use at your own risk.

Crashing Your App

Our first attempt was to crash the app after showing a UIAlert notifying the user that the app will be shutting down. Wasn’t too thrilled about this procedure but to accomplish this, basically you just throw an exception.

public class ExitAppException : Exception
    public ExitAppException() { }
    public ExitAppException(string message) : base(message) { }
    public ExitAppException(string message, Exception inner) :
          base(message, inner) { }

Then just call

throw new ExitAppException("known crash to exit app");

Does what it needs to, but crashing your app to exit it is not the best way to make it happen.

Calling exit() Function

Next option is to P/Invoke the exit() function. To accomplish this using C# do the following

[DllImport("__Internal", EntryPoint = "exit")]
public static extern void exit(int status);

Then just call the function

// show a UIAlert with yes no
exit(0); // if user clicked yes

Calling this may cause your app to fail App Store Certification and not the best user experience. It also will cause applicationWillTerminate and some UIApplicationDelegate methods to not be called. Here is an excerpt from iOS documentation


Using terminateWithSuccess

Third option is to call terminateWithSuccess which is a private method of (UIApplication *)sharedApplication.

Using Xamarin.iOS you basically have to use a Selector to call the private method as follows

static void TerminateWithSuccess ()
Selector selector = new Selector ("terminateWithSuccess");
    (selector, UIApplication.SharedApplication, 0); 

// call the method from somewhere

Calling private methods is not allowed according to Apple certification guidelines and you will most likely get you rejected from the app store.

Using NSThread.exit

Last option is to call NSThread.exit from your main thread.

// Somewhere in main thread

According to the documentation, you should avoid calling this because it doesn’t give you a chance to clean up or possibly save state.

So What to Use?!?

So typically in an iOS app you don’t usually ‘exit’ your app (same goes for Windows Phone and Android it’s a free for all) but there are sometimes situations where this is required. If you are creating an enterprise deploy app, you should be fine. If you are creating an app that will be put through the certification process, you may not pass with some of the options used. We have never had to use this ‘feature’ in an App Store app but my order in which I would try would be

  1. P/Invoke exit()
  2. NSThread.exit()
  3. throwing an exception
  4. terminateWithSuccess

Thanks go out to the Xamarin support team (specifically Brendan Zagaeski) for pointing the team in the right direction.

Join the discussion 3 Comments

  • […] go in more detail in the post titled Exiting an iOS App using Xamarin on […]

  • Will says:

    Enterprise deploy or not, I believe that this is a bad design decision. A client that asks for non-standard functionality should be educated as to why the ask is not ideal. It is the responsibility of developers using tools like Xamarin to understand best practices for iOS applications and honor them. These guidelines exist for a reason.

    • Mark Arteaga says:

      Agree it’s not the greatest design choice, but after discussing with customer and educating them they have the final say. We are not the only ones doing this, others have done this, just check stack overflow. As you can see by the examples, 3 out of 4 examples use Native iOS APIs to accomplish the task so it’s the responsibility of all developers to educate their customer no matter what the platform. But in the end, customer has the final decision even if you don’t agree with it.

Leave a Reply