2014-04-30 36 views
-2

我通過以下方法退出的iOS應用程序編程與UIAlertView中

-(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中止我的iOS應用:

-(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()方法會拒絕我的應用程序嗎?

謝謝!

+0

是的,它可能會 –

+0

https://developer.apple.com/library/ios/qa/qa1561/_index.html – rdurand

+0

這將是50- 50。他們可能會也可能不會注意到它。其他方式是 - 讓它退出,做一些意想不到的事情 - 在'nil'上調用方法...這樣,應用程序就會崩潰並退出。 – Michal

回答

4

是的,一般來說你會因此而被拒絕。

只需用單一選項向用戶顯示警報,以便他們必須批准才能關閉警報。然後,如果他們解僱(批准)他們可以使用該應用程序,如果他們不這樣做,他們不能手動並且必須手動退出該應用程序。

+0

我假設你真的想引出用戶的某種認可。如果你真的提供了一個「關閉」按鈕,那麼你應該刪除它並且不做任何事情。 – Wain

8

QA1561

問:如何以編程方式退出我的iPhone應用程序?

答:沒有提供API來優雅地終止iOS 應用程序。

在iOS中,用戶按下主頁按鈕關閉應用程序。如果您的應用程序具有無法提供其 預期功能的條件,建議的方法是顯示用戶提醒 ,指出問題的性質以及用戶可能採取的可能操作 - 啓用WiFi,啓用位置服務, 等允許用戶終止在他們自己的 酌情權的應用程序。

+1

我剛剛評論過相同的鏈接,大聲笑 – rdurand

1

在您alertview按鈕點擊

您可以使用:exit(0)

或者,

[[NSThread mainThread] exit],使用此您可以退出iOS應用。

+0

似乎[[NSThread mainThread] exit]甚至不會在11.2中編譯? – SAHM

2

可以使用以下代碼來退出的iOS應用編程與UIAlertView中: -

步驟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,,,,, 

}

感謝,

3
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]; 

使用出口(0)用於關閉當前應用程序

+0

警告:請勿調用退出功能。調用退出的應用程序對用戶來說會出現崩潰,而不是執行正常的終止和動畫回到主屏幕。 https://developer.apple.com/library/content/qa/qa1561/_index.html – seggy

0

如何調用fatalError() 功能?我剛剛使用它,一切都按預期工作。 (希望這不會引起拒絕。)

2

是的,上面的代碼會導致拒絕。使用此代碼,而不是你的戒備OK按鈕:

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