ios 强制退出程序APP代码

exit(1)、abort()、assert(0);

abort: 这是默认的程序结束函数,这种方式可能会或可能不会以刷新与关闭打开的文件或删除临时文件,这与你的设计有关.                            

exit: 附加了关闭打开文件与返回状态码给执行环境,并调用你用atexit注册的返回函数                                                                                 

assert(1) 为oc中的宏,只在debug模式下有用,当条件成立时,程序不会终止掉;当条件不成立时,程序终止。

警告:不要使用exit函数,调用exit会让用户感觉程序崩溃了,不会有按Home键返回时的平滑过渡和动画效果;另外,使用exit可能会丢失数据,因为调用exit并不会调用-applicationWillTerminate:方法和UIApplicationDelegate方法

如果在开发或者测试中确实需要强行终止程序时,推荐使用abort 函数和assert宏;

exit 方法

- (void)exitApplication{
   AppDelegate *app = [UIApplication sharedApplication].delegate;
   UIWindow*window = app.window;//动画
   [UIView animateWithDuration:1.0f animations:^{
     window.alpha = 0;  
    window.frame = CGRectMake(0, window.bounds.size.width, 0, 0);
       }completion:^(BOOLfinished) {
       exit(0);}];
   //exit(0);
}



abort 方法

- (void)exitApplication{
     #pragma clang diagnostic push
     #pragma clang diagnostic ignored "-Wundeclared-selector"
     //运行一个不存在的方法,退出界面更加圆滑
     [self performSelector:@selector(notExistCall)];
     abort();
     #pragma clang diagnostic pop
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容