用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 :
问:如何以编程方式退出我的iOS应用程序?
答:没有提供优雅地终止iOS应用程序的API。
在iOS中,用户按下主页buttonclosures应用程序。 如果您的应用程序具有不能提供其预期function的条件,则build议的方法是向用户显示警告,指出问题的性质以及用户可能采取的行动 – 打开WiFi,启用定位服务等。允许用户自行决定终止应用程序。
是的,一般来说你会因为这个而被拒绝。
只需用一个单选项向用户显示一个警报,所以他们必须批准才能解除警报。 然后,如果他们解雇(批准)他们可以使用该应用程序,如果他们不能,他们不能,并必须手动退出应用程序。
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)来closures当前应用程序
您可以使用下面的代码通过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,,,,,
}
谢谢,
是的,上面的代码会导致拒绝。 使用此代码,而不是在警报的确定button:
UIControl().sendAction(#selector(URLSessionTask.suspend), to: UIApplication.shared, for: nil)
在你的alertview
button上单击
你可以使用: exit(0)
?
要么,
[[NSThread mainThread] exit]
,使用这个你可以退出iOS应用程序。
如何调用fatalError()
函数? 我刚刚使用它,一切都按预期工作。 (希望这不会造成拒绝。)