2011-06-28 82 views
1

在iOS4的,我註冊了以下通知:如何在IOS4中處理UIApplicationWillTerminateNotification?

[[NSNotificationCenter defaultCenter] addObserver:self 
               selector:@selector(appWillTerminate:) 
               name:UIApplicationWillTerminateNotification 
               object:[UIApplication sharedApplication]]; 

-(void)appWillTerminate:(UIApplication *) app { 
    NSLog(@"terminate"); 
} 

我知道,這是不是在所有的情況稱爲像在上一個版本的iOS然而,簡單地通過註冊這個我得到一個EXC_BAD_ACCESSS當應用程序退出。不過,我無法刪除它,因爲我的應用程序支持iOS3.0 +。我該如何處理?

更新:這裏是崩潰日誌:

Exception Type: EXC_BAD_ACCESS (SIGBUS) 
Exception Codes: KERN_PROTECTION_FAILURE at 0x00000011 
Crashed Thread: 0 

Thread 0 Crashed: 
0 libobjc.A.dylib     0x0000441c objc_msgSend + 20 
1 Foundation      0x00015432 _nsnote_callback + 150 
2 CoreFoundation     0x000271da __CFXNotificationPost_old + 390 
3 CoreFoundation     0x00026e7a _CFXNotificationPostNotification + 122 
4 Foundation      0x00004720 -[NSNotificationCenter postNotificationName:object:userInfo:] + 64 
5 Foundation      0x0000de3a -[NSNotificationCenter postNotificationName:object:] + 14 
6 UIKit       0x000bef10 -[UIApplication _terminateWithStatus:] + 164 
7 UIKit       0x000be1b0 -[UIApplication _handleApplicationSuspend:eventInfo:] + 1980 
8 UIKit       0x0007e4a0 -[UIApplication handleEvent:withNewEvent:] + 3620 
9 UIKit       0x0007d470 -[UIApplication sendEvent:] + 60 
10 UIKit       0x0007ccf8 _UIApplicationHandleEvent + 6804 
11 GraphicsServices    0x00005dd8 PurpleEventCallback + 1024 
12 CoreFoundation     0x00035e40 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 22 
13 CoreFoundation     0x00035dfe __CFRunLoopDoSource1 + 158 
14 CoreFoundation     0x0002809e __CFRunLoopRun + 574 
15 CoreFoundation     0x00027d74 CFRunLoopRunSpecific + 220 
16 CoreFoundation     0x00027c82 CFRunLoopRunInMode + 54 
17 GraphicsServices    0x00004e84 GSEventRunModal + 188 
18 UIKit       0x00004f8c -[UIApplication _run] + 564 
19 UIKit       0x000024cc UIApplicationMain + 964 
+0

EXC_BAD_ACCESS可能不相關。我不認爲'UIApplicationWillTerminateNotification'是在支持任務的設備上發送的。你有'applicationWillEnterBackground:'中的任何代碼還是相應的通知? –

+0

奇怪的是,我刪除了'applicationWillEnterBackground'中的所有代碼,並且仍然發生同樣的情況,最重要的是,如果我刪除了'UIApplicationWillTerminateNotification'的通知,那麼所有代碼​​都可以正常工作。此外,這隻發生在設備上,而不是在模擬器上。將使用崩潰日誌更新問題。 –

回答

1

確保您刪除任何正在偵聽終止通知的類中的觀察者。如果您沒有刪除通知,它會嘗試將其發佈到解除分配的對象,然後您的應用程序將崩潰。你不應該因爲聽一個不存在的通知而崩潰,它不會被調用。

//YourClass 
-(id)init 
{ 
    if((self = [super init])) 
    { 
     [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(appWillTerminate:) 
              name:UIApplicationWillResignActiveNotification 
              object:[UIApplication sharedApplication]]; 
    } 
    return self; 
} 

-(void)appWillTerminate:(NSNotification *)note { 
    NSLog(@"terminate"); 
} 

-(void)dealloc 
{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 

    //Other releases 
    [super dealloc]; 
} 
+1

好的,這是非常有意義的,當然它是有效的。但是,如果我刪除觀察者,終止方法根本不會被調用(自然)......但我仍然需要響應'UIApplicationWillTerminateNotification'通知。我應該把它放在不同的課堂上嗎? –

+0

我在應用程序委託中添加了我需要的調用並開始工作。謝謝您的幫助。 –

+0

是的,它是它的地方,它已經支持該方法,無需添加通知觀察者。 – Joe

0

你可以做的是發現了iOS版本,如果它的3.0版本,然後使用appWillTerminate如果第V 4.0使用appWillResignActive或appDidEnterBackground,例如。 :

NSString *ver = [[UIDevice currentDevice] systemVersion]; 
if([ver isEqualToString:@"3.0"]){ 
    //Device is running 3.0 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(appWillTerminate:) 
              name:UIApplicationWillTerminateNotification 
              object:[UIApplication sharedApplication]]; 
} 
else if([ver isEqualToString:@"4.0"]){ 
    //4.0 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(appWillTerminate:) 
              name:UIApplicationWillResignActiveNotification 
              object:[UIApplication sharedApplication]]; 
} 
+0

只是改變了我的答案,意識到這是錯誤的大聲笑。 –

相關問題