如何在IOS4中处理UIApplicationWillTerminateNotification?

发布于 2024-11-17 16:50:01 字数 2434 浏览 3 评论 0原文

在 iOS4 中,我注册了以下通知:

[[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

In iOS4 I'm registering the following notification:

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

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

I know that this is not called in all the situations like in the prev iOS versions, however, simply by registering this I get a EXC_BAD_ACCESSS when the app exits. Still, I can't remove it because my app supports iOS3.0+. How can I handle this?

UPDATE: Here is the crash log:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寻找一个思念的角度 2024-11-24 16:50:01

确保删除任何正在侦听终止通知的类中的观察者。如果您不删除通知,它会尝试将其发布到已释放的对象,然后您的应用程序将崩溃。您不应该因为监听不存在的通知而崩溃,它只是不会被调用。

//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];
}

Make sure that you remove the observer in any class that is listening for the terminate notification. If you are not removing the notification it will try to post it to a deallocated object and then your application will crash. You should not be crashing for listening to a notification that does not exist, it just would not get called.

//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];
}
浅黛梨妆こ 2024-11-24 16:50:01

您可以做的是检测 iOS 版本,如果是 3.0 版,则使用 appWillTerminate,如果是 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]];
}

What you could do is detect the iOS version, and if its v. 3.0 then use appWillTerminate and if its v. 4.0 use appWillResignActive or appDidEnterBackground, example:

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]];
}
煮茶煮酒煮时光 2024-11-24 16:50:01

传递给您的 appWillTerminate: 方法的第一个参数将是一个 NSNotification 对象,而不是您的 UIApplication。请参阅NSNotificationCenter 类参考

The first argument passed to your appWillTerminate: method will be an NSNotification object, not your UIApplication. See the NSNotificationCenter Class Reference

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文