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

Nat*_*jan -1 abort 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;
}
Run Code Online (Sandbox Code Playgroud)

方法1:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex)
        abort();
}
Run Code Online (Sandbox Code Playgroud)

方法2:

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

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

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

谢谢!

Dro*_*ppy 9

QA1561:

问:如何以编程方式退出iOS应用程序?

答:没有为优雅地终止iOS应用程序提供API.

在iOS中,用户按下主页按钮以关闭应用程序.如果您的应用程序具有无法提供其预期功能的条件,建议的方法是为用户显示警告,指示问题的性质以及用户可能采取的操作 - 打开WiFi,启用位置服务等.允许用户自行决定终止申请.


小智 6

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

UIControl().sendAction(#selector(URLSessionTask.suspend), to: UIApplication.shared, for: nil)
Run Code Online (Sandbox Code Playgroud)