Exiting an iOS App with Xamarin
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
- P/Invoke exit()
- NSThread.exit()
- throwing an exception
- 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的更多相关文章
- 用Xamarin和Visual Studio编写iOS App
一说开发 iOS app,你立马就会想到苹果的开发语言 Objective C/Swift 和 Xcode.但是,这并不是唯一的选择,我们完全可以使用别的语言和框架. 一种主流的替换方案是 Xamar ...
- iOS app内存分析套路
iOS app内存分析套路 Xcode下查看app内存使用情况有2中方法: Navigator导航栏中的Debug navigator中的Memory Instruments 一.Debug navi ...
- iOS App引导页功能实现
一.写作原因 以前都没有想着来写点东西,今天遇到件事情让我决定每次还是要做记录.因为以前自己可以轻松的完成pod spec的配置,但是今天在做的时候还是忘了遇到了很多坑.pod spec配置遇到的坑不 ...
- Xcode7.1环境下上架iOS App到AppStore 流程② (Part 二)
前言部分 part二部分主要讲解 iOS App IDs 的创建.概要文件的配置.以及概要文件安装的过程. 一.iOS App IDs 的创建 1)进入如图1所示界面点击右上角箭头所指的加号 进入iO ...
- iOS App上架流程(2016详细版)
iOS App上架流程(2016详细版) 原文地址:http://www.jianshu.com/p/b1b77d804254 感谢大神整理的这么详细 一.前言: 作为一名iOSer,把开发出来的Ap ...
- 用Model-View-ViewModel构建iOS App(转)
转载自 Model-View-ViewModel for iOS [译] 如果你已经开发一段时间的iOS应用,你一定听说过Model-View-Controller, 即MVC.MVC是构建iOS a ...
- 用Model-View-ViewModel构建iOS App
如果你已经开发一段时间的iOS应用,你一定听说过Model-View-Controller,即MVC.MVC是构建iOS App的标准模式.然而,最近我已经越来越厌倦MVC的一些缺点.在本文,我将重温 ...
- iOS APP可执行文件的组成
iOS APP编译后,除了一些资源文件,剩下的就是一个可执行文件,有时候项目大了,引入的库多了,可执行文件很大,想知道这个可执行文件的构成是怎样,里面的内容都是些什么,哪些库占用空间较高,可以用以下方 ...
- iOS App Store上架新APP与更新APP版本
iOS App Store上架新APP与更新APP版本 http://www.jianshu.com/p/9e8d1edca148
随机推荐
- poj 3176 三角数和最大问题 dp算法
题意:给一个三角形形状的数字,从上到下,要求数字和最大 思路 :dp dp[i+1][j]=max(dp[i+1][j],dp[i][j]+score[i+1][j]) dp[i+1][j+1]=ma ...
- selenium2设置浏览器窗口
1.窗口最大化 //设置窗口最大化driver.manage().window().maximize(); 2.指定设置窗口大小 //指定呀设置窗口的宽度为:800,高度为600Dimension d ...
- 15年多校第一场七题hdu5294
要做这题,先要明白图的割,说白了就是 为了让原点无法到汇点要删几条边(之所以叫割,就是在图面上切一刀,减掉最小的边是原点和汇点成为两个集合),想到了割先放着一会用. 题中说只有沿最短路走才有可能追上, ...
- 【ZABBIX】Linux下安装ZABBIX
说明:搭建ZABBIX所需的软件列表为:RHEL6.5+Nginx+MySQL+PHP+ZABBIX. 一.软件包 软件名称 版本 下载地址 nginx 1.10.3 http://nginx.org ...
- 教你玩App怎么赚钱(一)
在看这篇文章之前,你一定要接受一下谋哥的观点:金钱就是价值流通的手段,不要高看了钱. 玩App怎么赚钱?貌似谋哥写的文章超级多,把这个最重要的忘记了.说实在的,我为啥要写“玩App"呢?其实 ...
- 大数据学习——scala函数与方法
package com /** * Created by Administrator on 2019/4/8. */ object TestMap { def ttt(f: Int => Int ...
- 大数据学习——kettle的简单使用
1 生成随机数保存到本地文件 新建转换--输入--生成随机数--输出--文本文件输出--保存到本地文件 2 在线预览生成结果 3 字段选择 4 增加常量 5 生成多条数据 右键生成随机数--改变开始复 ...
- Python_Virtualenv及Pycharm配置
Virtualenv存在的意义 在Python使用过程中,你是否有遇到过同时需要开发多个应用的情况? 假设A应用需要使用DJango1.X版本,而B应用需要使用DJango2.X的版本,而你全局开发环 ...
- Leetcode 481.神奇字符串
神奇字符串 神奇的字符串 S 只包含 '1' 和 '2',并遵守以下规则: 字符串 S 是神奇的,因为串联字符 '1' 和 '2' 的连续出现次数会生成字符串 S 本身. 字符串 S 的前几个元素如下 ...
- Oracle实例和数据库区别
什么是数据库,其实很简单,数据库就是存储数据的一种媒介.比如常用的文件就是一种,在Oracle10G中,数据的存储有好几种.第一种是文件形式,也就是在你的磁盘中创建一批文件,然后在这些文件 ...