iOS 強制退出程式APP代碼

來源:互聯網
上載者:User

標籤:wan   nbsp   window   gre   結束   效果   animate   設計   erb   

1、先po代碼

// 退出程式 1

12345678910111213141516171819202122232425262728293031 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;     // 動畫 1    [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);     }
 退出程式 2://-------------------------------- 退出程式 2-----------------------------------------//- (void)exitApplication {    [UIView beginAnimations:@"exitApplication" context:nil];    [UIView setAnimationDuration:0.5];    [UIView setAnimationDelegate:self];    // [UIView setAnimationTransition:UIViewAnimationCurveEaseOut forView:self.view.window cache:NO];    [UIView setAnimationTransition:UIViewAnimationCurveEaseOut forView:self.window cache:NO];    [UIView setAnimationDidStopSelector:@selector(animationFinished:finished:context:)];    //self.view.window.bounds = CGRectMake(0, 0, 0, 0);    self.window.bounds = CGRectMake(0, 0, 0, 0);    [UIView commitAnimations];}- (void)animationFinished:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {    if ([animationID compare:@"exitApplication"] == 0) {        exit(0);    }}

 

 

2、程式中的exit(1)、abort()、assert(0);

 

先來看一下程式的死亡方式:

 程式的死亡大致有三種:自然死亡,即無疾而終,通常就是main()中的一個return 0;自殺,當程式發現自己再活下去已經沒有任何意義時,通常會選擇自殺。當然,這種自殺也是一種請求式的自殺,即請求OS將自己斃掉。有三種方式:void exit(int status)和void abort(void)、assert(condition)。他殺,同現實不同的是,程式家族中的他殺行徑往往是由自己至親完成的,通常這個至親就是他的生身父親(還是母親?)。語言本身並沒有提供他殺的兇器,這些兇器往往是由OS直接或者間接(通過一些進程庫,如pthread)提供的。 自然死是最完美的結局,他殺是我們最不願意看到的,自殺雖是迫不得已,但主動權畢竟還是由程式自己掌控的;abort被調用時,程式將直接退出,任何對象的解構函式都不會調用

 

介紹:

 abort: 這是預設的程式結束函數,這種方式可能會或可能不會以重新整理與關閉開啟的檔案
或刪除臨時檔案,這與你的設計有關.
exit: 附加了關閉開啟檔案與返回狀態代碼給執行環境,並調用你用atexit註冊的返回函數

assert(1)為oc中的宏,只在debug模式下有用,當條件成立時,程式不會終止掉;當條件不成立時,程式終止。

so,oc程式中建議用assert(condition)函數。

 

3、選擇

Q:怎樣用代碼方式退出iOS程式

 

       A:沒有提供用於正常退出IOS應用的API。

 

       在IOS中,使用者點擊Home鍵來關閉應用。你的應用應該符合以下條件:它不能自行調用方法,而應採取措施與使用者互動,表明問題的性質和應用可能會採取的行為,比如開啟WIFI,使用定位服務等供使用者選擇確定使用;

 

       警告:不要使用exit函數,調用exit會讓使用者感覺程式崩潰了,不會有按Home鍵返回時的平滑過渡和動畫效果;另外,使用exit可能會遺失資料,因為調用exit並不會調用-applicationWillTerminate:方法和UIApplicationDelegate方法;

 

如果在開發或者測試中確實需要強行終止程式時,推薦使用abort 函數和assert宏;

 

iOS 強制退出程式APP代碼

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.