iphoneios4backgroundnotificationsios-3.x

What happens when observing UIApplicationDidEnterBackgroundNotification on old iOS without multitasking?


I have this line of code:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stop) name:UIApplicationDidEnterBackgroundNotification object:nil];

Works fine in iPhone 4.3 simulator and on my iPhone running 4.x. However a crash occurs there on an iPhone running 3.x. Makes sense. My guess is that that line of code doesn't work when there is no multitasking.

It's difficult for me to debug because the person testing for me with the 3.x system is in a remote location. I'm not even sure whether the symbol UIApplicationDidEnterBackgroundNotification evaluates to the correct string, or nil, or random uninitialized memory, or what on that OS.

But beyond figuring out why it's failing, which I can do with some effort, what am I supposed to do? How do I detect whether a particular notification exists before I observe it? Or do I check if multitasking is available as a general category? I guess I thought the line of code would be safe. If the OS never generates that notification, then I don't get notified, but the line shouldn't crash.


Solution

  • You should check whether the iOS version is 4.0 or later when compiling (there are inbuilt preprocessor #defines for this) and test multitasking support as well (in case it's disabled on the device when it would otherwise be supported):

    #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
        if([[UIDevice currentDevice]
            respondsToSelector:@selector(isMultitaskingSupported)] &&
           [[UIDevice currentDevice] isMultitaskingSupported])
        {
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stop) name:UIApplicationDidEnterBackgroundNotification object:nil];
        }
    #endif
    

    You don't have to use the #if, but it saves compiling in extra checks which you know will always return false.