iOS 代码关闭App

程序的死亡大致有三种:自然死亡,即无疾而终,通常就是main()中的一个return 0;自杀,当程序发现自己再活下去已经没有任何意义时,通常会选择自杀。当然,这种自杀也是一种请求式的自杀,即请求OS将自己毙掉

方法一.

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"退!出!" message:@"退出APP" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"退出", nil];

    [alert show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    if(buttonIndex ==1){

        [self exitApplication];
    }

}

-(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:^(BOOL finished) {

            exit(0);

        }];

        //exit(0);

}


方法二.

#pragma mark - 第二种方法
- (void)exitApplication1{

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

    [UIView setAnimationDuration:0.5];

    [UIView setAnimationDelegate:self];

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

    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

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

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

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

    delegate.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);
    }

}


项目 demo 下载,喜欢的朋友请给个 start

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小毅哥哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值