iOS 强制退出程序APP代码

1、先po代码

// 退出程序 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
UIAlertView* alert = [[UIAlertView alloc] initWithTitle: self .exitapplication message:@ ""  delegate: self  cancelButtonTitle: self .exityes otherButtonTitles: self .exitno, nil ];
         
   [alert show];
 
 
- ( void )alertView:(UIAlertView *)alertView clickedButtonAtIndex:( NSInteger )buttonIndex
 
{
     if (buttonIndex ==0){
         
         
         [ self  exitApplication ];
         
     }
 
}
 
- ( void )exitApplication {
     
     AppDelegate *app = [UIApplication sharedApplication].delegate;
     UIWindow *window = app.window;
     // 动画 1
     [UIView animateWithDuration:1.0f animations:^{
         window.alpha = 0;
         window.frame = CGRectMake(0, window.bounds.size.width, 0, 0);
     } completion:^( BOOL  finished) {
         exit(0);
     }];
     //exit(0);
     
}
 退出程序 2:
//-------------------------------- 退出程序 2-----------------------------------------//

- (void)exitApplication {

    [UIView beginAnimations:@"exitApplication" context:nil];

    [UIView setAnimationDuration:0.5];

    [UIView setAnimationDelegate:self];

    // [UIView setAnimationTransition:UIViewAnimationCurveEaseOut forView:self.view.window cache:NO];

    [UIView setAnimationTransition:UIViewAnimationCurveEaseOut forView:self.window cache:NO];

    [UIView setAnimationDidStopSelector:@selector(animationFinished:finished:context:)];

    //self.view.window.bounds = CGRectMake(0, 0, 0, 0);

    self.window.bounds = CGRectMake(0, 0, 0, 0);

    [UIView commitAnimations];

}



- (void)animationFinished:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {

    if ([animationID compare:@"exitApplication"] == 0) {

        exit(0);

    }

}

 

 

2、程序中的exit(1)、abort()、assert(0);

 

先来看一下程序的死亡方式:

 程序的死亡大致有三种:自然死亡,即无疾而终,通常就是main()中的一个return 0;自杀,当程序发现自己再活下去已经没有任何意义时,通常会选择自杀。当然,这种自杀也是一种请求式的自杀,即请求OS将自己毙掉。有三种方式:void exit(int status)和void abort(void)、assert(condition)。他杀,同现实不同的是,程序家族中的他杀行径往往是由自己至亲完成的,通常这个至亲就是他的生身父亲(还是母亲?)。语言本身并没有提供他杀的凶器,这些凶器往往是由OS直接或者间接(通过一些进程库,如pthread)提供的。 自然死是最完美的结局,他杀是我们最不愿意看到的,自杀虽是迫不得已,但主动权毕竟还是由程序自己掌控的;abort被调用时,程序将直接退出,任何对象的析构函数都不会调用

 

介绍:

 abort: 这是默认的程序结束函数,这种方式可能会或可能不会以刷新与关闭打开的文件
或删除临时文件,这与你的设计有关.
exit: 附加了关闭打开文件与返回状态码给执行环境,并调用你用atexit注册的返回函数

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

so,oc程序中建议用assert(condition)函数。

 

3、选择

Q:怎样用代码方式退出iOS程序

 

       A:没有提供用于正常退出IOS应用的API。

 

       在IOS中,用户点击Home键来关闭应用。你的应用应该符合以下条件:它不能自行调用方法,而应采取措施与用户交互,表明问题的性质和应用可能会采取的行为,比如打开WIFI,使用定位服务等供用户选择确定使用;

 

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

 

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

 

转载于:https://www.cnblogs.com/lurenq/p/7800378.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值