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

时间:2014-04-30 15:07:39

标签: ios uialertview abort

我通过以下方法中止我的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()方法是否会拒绝我的应用?

谢谢!

7 个答案:

答案 0 :(得分:8)

请参阅QA1561

  

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

     

答:没有提供API来优雅地终止iOS   应用

     

在iOS中,用户按Home键关闭应用程序。应该   您的申请具有无法提供的条件   预期的功能,推荐的方法是显示警报   指示问题性质和可能操作的用户   用户可以 - 打开WiFi,启用位置服务,   等允许用户自行终止应用程序   决定。

答案 1 :(得分:4)

是的,通常你会被拒绝。

只需向用户发出一个警告选项,因此他们必须批准解除警报。然后,如果他们解雇(批准)他们可以使用该应用程序,如果他们不能,他们就不能手动退出应用程序。

答案 2 :(得分:4)

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

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

答案 3 :(得分:3)

'utf8'

使用退出(0)进行关闭当前应用

答案 4 :(得分:2)

您可以使用以下代码以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,,,,,

}

感谢,

答案 5 :(得分:1)

点击alertview按钮

您可以使用:exit(0)

或者,

[[NSThread mainThread] exit],使用此功能可以退出ios app。

答案 6 :(得分:-1)

如何致电fatalError() 功能?我刚刚使用它,一切都按预期工作。 (希望这不会导致拒绝。)