使用UIAlertView以编程方式退出iOS应用程序



我将通过以下方法中止我的iOS应用程序

-(void)cancelSelected
{
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:nil message:@"Are you sure you want to exit?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
    [alert show];
    alert = nil;
}

方法1:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex)
        abort();
}

方法2:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex)
        [NSException raise:@"Disagree terms and conditions." format:@"Quit, Cancel"];
}

我应该这样做以编程方式退出我的iOS应用程序吗?

这种abort()方法会导致拒绝我的应用程序吗?

谢谢!

参见QA1561:

Q: 如何以编程方式退出我的iOS应用程序?

A: 没有提供API来正常终止iOS应用

在iOS中,用户按下主页按钮关闭应用程序。应该您的申请存在无法提供其预期功能,建议的方法是显示指示问题性质和可能的操作的用户用户可以打开WiFi、启用定位服务,等等。允许用户自行终止应用程序自由裁量权

是的,上面的代码将导致拒绝。在警报的OK按钮中使用此代码:

UIControl().sendAction(#selector(URLSessionTask.suspend), to: UIApplication.shared, for: nil)

是的,通常您会因此而被拒绝。

只需向用户提供一个带有单一选项的警报,因此他们必须批准取消警报。然后,如果他们拒绝(批准(,他们可以使用该应用程序,如果不这样做,他们就不能,必须手动退出该应用程序。

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Game Over" message:@"Your time is up" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *close = [UIAlertAction actionWithTitle:@"close" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
                                                                 **exit(0);**
                                                             }];
UIAlertAction *playagain = [UIAlertAction actionWithTitle:@"Play again" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
                                                                  [self viewDidLoad];
                                                             }];
[alert addAction:close];
[alert addAction:playagain];
[self presentViewController:alert animated:YES completion:nil];

使用exit(0(关闭当前应用程序

您可以使用以下代码使用UIAlertView以编程方式退出iOS应用程序:-

步骤1:

Delegate "UIAlertViewDelegate" to your viewcontroller.h
for example:
 @interface User_mail_List : UIViewController< UIAlertViewDelegate >

步骤2:

//create your UIAlertView
UIAlertView  *exit_alertView= [[UIAlertView alloc] initWithTitle:@"Bizbilla !" message:@"nAre you sure you want to Exit ?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes",nil];
[exit_alertView show];

步骤3:

-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex{
if(alertView==exit_alertView){//exit Alert Fns Start,,,,,
    if(buttonIndex==1){
        exit(0);
    }
}//exit Alert Fns End,,,,,

}

谢谢,

alertview按钮上单击

您可以使用:exit(0)

或者,

[[NSThread mainThread] exit],使用它可以退出ios应用程序。

调用fatalError()怎么样作用我刚刚用过,一切都如预期。(希望这不会导致拒绝。(

最新更新