Exiting an iOS App with Xamarin

Mark Arteaga

|

April 16, 2014

Mark Arteaga

April 16, 2014

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


ios-warning


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");

UIApplication.SharedApplication.PerformSelector

   (selector, UIApplication.SharedApplication, 0);

}

// call the method from somewhere

TerminateWithSuccess();


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

NSThread.exit();


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.

Latest Posts

Let's Make Something Great Together

Contact Us

About Us