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.

Exiting an iOS App with Xamarin的更多相关文章

  1. 用Xamarin和Visual Studio编写iOS App

    一说开发 iOS app,你立马就会想到苹果的开发语言 Objective C/Swift 和 Xcode.但是,这并不是唯一的选择,我们完全可以使用别的语言和框架. 一种主流的替换方案是 Xamar ...

  2. iOS app内存分析套路

    iOS app内存分析套路 Xcode下查看app内存使用情况有2中方法: Navigator导航栏中的Debug navigator中的Memory Instruments 一.Debug navi ...

  3. iOS App引导页功能实现

    一.写作原因 以前都没有想着来写点东西,今天遇到件事情让我决定每次还是要做记录.因为以前自己可以轻松的完成pod spec的配置,但是今天在做的时候还是忘了遇到了很多坑.pod spec配置遇到的坑不 ...

  4. Xcode7.1环境下上架iOS App到AppStore 流程② (Part 二)

    前言部分 part二部分主要讲解 iOS App IDs 的创建.概要文件的配置.以及概要文件安装的过程. 一.iOS App IDs 的创建 1)进入如图1所示界面点击右上角箭头所指的加号 进入iO ...

  5. iOS App上架流程(2016详细版)

    iOS App上架流程(2016详细版) 原文地址:http://www.jianshu.com/p/b1b77d804254 感谢大神整理的这么详细 一.前言: 作为一名iOSer,把开发出来的Ap ...

  6. 用Model-View-ViewModel构建iOS App(转)

    转载自 Model-View-ViewModel for iOS [译] 如果你已经开发一段时间的iOS应用,你一定听说过Model-View-Controller, 即MVC.MVC是构建iOS a ...

  7. 用Model-View-ViewModel构建iOS App

    如果你已经开发一段时间的iOS应用,你一定听说过Model-View-Controller,即MVC.MVC是构建iOS App的标准模式.然而,最近我已经越来越厌倦MVC的一些缺点.在本文,我将重温 ...

  8. iOS APP可执行文件的组成

    iOS APP编译后,除了一些资源文件,剩下的就是一个可执行文件,有时候项目大了,引入的库多了,可执行文件很大,想知道这个可执行文件的构成是怎样,里面的内容都是些什么,哪些库占用空间较高,可以用以下方 ...

  9. iOS App Store上架新APP与更新APP版本

    iOS App Store上架新APP与更新APP版本 http://www.jianshu.com/p/9e8d1edca148

随机推荐

  1. 记一次虚拟机无法挂载volume的怪异问题排查

    故障现象 使用nova volume-attach <server> <volume>命令挂载卷,命令没有返回错误,但是查看虚拟机状态,卷并没有挂载上. 故障原因 疑似虚拟机长 ...

  2. selenium2等待元素加载

    1.硬性等待 Thread.sleep(8000); 所谓的硬性等待就是,执行完相应操作就等待我设置的8s.无论网速快与慢,网速快的话,也许5s就打开网页了,可是程序必须接着等待剩下的3秒. 网速慢的 ...

  3. HDU 4565 So Easy! 矩阵快速幂

    题意: 求\(S_n=\left \lceil (a+\sqrt{b})^n \right \rceil mod \, m\)的值. 分析: 设\((a+\sqrt{b})^n=A_n+B_n \sq ...

  4. luogu1208 尼克的任务

    倒着推就是了 #include <iostream> #include <cstdio> #include <vector> using namespace std ...

  5. 如何安装mongodb.msi

    到MongoDB官网下载MongoDB软件:mongodb-win32-x86_64-2008plus-ssl-3.0.2-signed.msi, 放在想要安装的地方: 如:d:\MongoDB\ 2 ...

  6. cobbler常用目录/命令(三)

    常用目录: /var/www/cobbler/ks_mirror/                cobbler distro文件目录 /var/lib/tftpboot/pxelinux.cfg/d ...

  7. git命令综合

    Git 是一个很强大的分布式版本控制系统.它不但适用于管理大型开源软件的源代码,管理私人的文档和源代码也有很多优势.Git常用操作命令:1) 远程仓库相关命令检出仓库:$ git clone git: ...

  8. 【bzoj3291】Alice与能源计划 模拟费用流+二分图最大匹配

    题目描述 在梦境中,Alice来到了火星.不知为何,转眼间Alice被任命为火星能源部长,并立刻面临着一个严峻的考验. 为了方便,我们可以将火星抽象成平面,并建立平面直角坐标系.火星上一共有N个居民点 ...

  9. 二进制<4>

    位运算简介及实用技巧(四):实战篇 下面分享的是我自己写的三个代码,里面有些题目也是我自己出的.这些代码都是在我的Pascal时代写的,恕不提供C语言了.代码写得并不好,我只是想告诉大家位运算在实战中 ...

  10. FZOJ Problem 2150 Fire Game

                                                                                                        ...