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
随机推荐
- MIP启发式算法:Variable neighborhood search
*本文主要记录和分享学习到的知识,算不上原创. *参考文章见链接. 本文主要讲述启发式算法中的变邻域搜索(Variable neighborhood search).变邻域搜索的特色在于邻域结构的可变 ...
- selenium2元素定位Xpath和cssSelector
Selenium2中元素有以下几种定位方法, 常用的有Id,xpath, cssSelector XPATH介绍: XPATH是一种选择器 XPATH在firefox中用firepath验证 XP ...
- VS2017生成.net core项目报错:The current .NET SDK does not support targeting .NET Core 2.1. Either
今天在生成一个项目的时候,生成报错,错误如下:The current .NET SDK does not support targeting .NET Core 2.1. Either target ...
- UVa 12167 & HDU 2767 强连通分量 Proving Equivalences
题意:给出一个有向图,问最少添加几条有向边使得原图强连通. 解法:求出SCC后缩点,统计一下出度为0的点和入度为0的点,二者取最大值就是答案. 还有个特殊情况就是本身就是强连通的话,答案就是0. #i ...
- webdriver高级应用- 禁止IE的保护模式
#encoding=utf-8 from selenium import webdriver from selenium.webdriver.common.desired_capabilities i ...
- ABP介绍
ABP是ASP.NET Boilerplate Project (ASP.NET样板项目)的简称. ABP 适用的场景:中小规模 WEB 应用开发,可直接使用 ABP 框架. 较大型项目可以在 ABP ...
- 九度oj 1006
题目1006:ZOJ问题 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:20252 解决:3544 题目描述: 对给定的字符串(只包含'z', ...
- 读懂diff(转载)
作者: 阮一峰 日期: 2012年8月29日 diff是Unix系统的一个很重要的工具程序. 它用来比较两个文本文件的差异,是代码版本管理的基石之一.你在命令行下,输入: $ diff <变动前 ...
- ACM程序设计选修课——1076汇编语言(重定向+模拟)
1076: 汇编语言 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 34 Solved: 4 [Submit][Status][Web Board] ...
- input和textarea区别
1. input是单行文本,textarea是多行文本,可以带滚动条2. input的value放在标签里面 <input type="text" value="b ...