轉自:http://blog.csdn.net/m372897500/article/details/36896015
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:self.exitapplication message:@"" delegate:self cancelButtonTitle:self.exityes otherButtonTitles:self.exitno,nil];
[alert show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex ==0){
[self exitApplication ];
}
}
- (void)exitApplication {
AppDelegate *app = [UIApplication sharedApplication].delegate;
UIWindow *window = app.window;
[UIView animateWithDuration:1.0f animations:^{
window.alpha = 0;
window.frame = CGRectMake(0, window.bounds.size.width, 0, 0);
} completion:^(BOOL finished) {
exit(0);
}];
//exit(0);
}
Q:怎樣用代碼方式退出IOS程序
A:沒有提供用於正常退出IOS應用的API。
在IOS中,用戶點擊Home鍵來關閉應用。你的應用應該符合以下條件:它不能自行調用方法,而應採取措施與用戶交互,表明問題的性質和應用可能會採取的行爲,比如打開WIFI,使用定位服務等供用戶選擇確定使用;
警告:不要使用exit函數,調用exit會讓用戶感覺程序崩潰了,不會有按Home鍵返回時的平滑過渡和動畫效果;另外,使用exit可能會丟失數據,因爲調用exit並不會調用-applicationWillTerminate:方法和UIApplicationDelegate方法;
如果在開發或者測試中確實需要強行終止程序時,推薦使用abort 函數和assert宏;