I have my application working great on my iOS 4.3.3 iPhone 3GS. When I test the app on a 3.1.3 iPhone 3G, the program crashes right after the splash image is shown. The debugger points to the last command of my root view controller’s awakeFromNib:
- (void)awakeFromNib
{
NSLog(@"awakeFromNib");
NSLog(@"applicationWillEnterForeground listened");
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillEnterForeground:)
name:UIApplicationWillEnterForegroundNotification
object: nil];
}
.
2011-08-09 15:56:24.585 AppName[4401:207] awakeFromNib
2011-08-09 15:56:24.602 AppName[4401:207] applicationWillEnterForeground listened
Program received signal: “EXC_BAD_ACCESS”.
Is there something special about iOS 3’s awaking/sleeping that I should know that would help me work around this problem?
The problem is that the identifier
UIApplicationWillEnterForegroundNotificationis pointing to an externally defined string that only exists on iOS 4 or later. On iOS 3 and earlier, it will evaluate to nil; thus, you are passing in nil for a notification name, which is why adding the observer is crashing.You can fix this in two ways. You could directly use the string value of the notification’s name in your code:
I’m not sure if that’s what it is, you’ll have to check the docs or use NSLog to be exactly sure of it.
A better option is to check the value of the identifier first, and only add a listener if it is supported: