referenced from: http://www.redbitdev.com/exiting-ios-app-with-xamarin-ios/

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.

1
2
3
4
5
6
7
public class ExitAppException : Exception
{
    public ExitAppException() { }
    public ExitAppException(string message) : base(message) { }
    public ExitAppException(string message, Exception inner) :
          base(message, inner) { }
}

Then just call

1
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

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

Then just call the function

1
2
// 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

1
2
3
4
5
6
7
8
9
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.

1
2
// 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.

最新文章

  1. 五分钟搭建起一个包含CRUD功能的JqGrid表格
  2. UITabBarController 、TabBar背景颜色设置,UITabBarItem的文字样式(颜色和大小)UITabBarItem的位置调整
  3. 黑马.net12期视频教程
  4. jquery火焰等效果导航菜单
  5. 耳机jack构造及在应用时可能出现的问题
  6. JDBC 事务控制
  7. CoreLocation框架的使用
  8. 李洪强漫谈iOS开发[C语言-025]-赋值运算符案例
  9. Triangle LOVE(拓扑排序)
  10. [USACO08JAN]手机网络Cell Phone Network
  11. 1.Cocos2d-x-3.2编写3d打飞机,粒子管理器代码
  12. 第三章:shiro授权认证
  13. 深入学习css伪类和伪元素及其用法
  14. ZOL 3977. Pointers
  15. [CERC2014] Virus synthesis
  16. 服务注册中心之ZooKeeper系列(三) 实现分布式锁
  17. 关于SQL查询语句中的LIKE模糊查询的解释
  18. service mysqld start,Failed to start mysqld.service: Access denied
  19. python和redis简单交互
  20. linux上open-vswitch安装和卸载

热门文章

  1. Android兼容性测试CTS Verifier-环境搭建、测试执行、结果分析
  2. 文件上传下载,命令之wget / curl / which / sort / uniq / cut / wc /tr /sed
  3. Django基于Pycharm开发之二 [使用django adminSite]
  4. loj2280 「FJOI2017」矩阵填数
  5. Leetcode1--->数组中两数之和等于给定数
  6. 零基础学 JavaScript 全彩版 明日科技 编著
  7. LaTeX Hierarchical Tables
  8. pat 1036
  9. 视频播放插件Video.js
  10. 高并发下的HashMap,ConcurrentHashMap