iOS 程式碼關閉App

小毅哥哥發表於2017-08-17

程式的死亡大致有三種:自然死亡,即無疾而終,通常就是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

相關文章