I created a new window-based project and couldn’t figure out why it wasn’t doing anything. Eventually I put an NSLog right after didFinishLaunching and it’s never logged when I run it. Here is all of the code I have written:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog(@"didFinishLaunching");
// Get the device object and turn proximity monitoring on
UIDevice *d = [UIDevice currentDevice];
[d setProximityMonitoringEnabled:YES];
// Get the NSNotificationCenter object
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
// Sign up to receive notifications when the proximity state changes
[nc addObserver:self selector:@selector(proximityChanged:) name:UIDeviceProximityStateDidChangeNotification object:d];
NSLog(@"Observing...");
[self.window makeKeyAndVisible];
return YES;
}
- (void)proximityChanged:(NSNotification *)note {
// Print out the changes of proximity state
NSLog(@"Proximity Changed: %d", [[note object] proximityState]);
}
That’s the entirety of what I’ve written and nothing is logged when I run it on the simulator or on my device. Any thoughts?
Well, you are talking about Application Delegate. The obvious reason – your object is not set as an application delegate.
Looking at Apple documentation there is quite a few ways to accomplish it:
Check you nib file in Interface Builder and see if the App Delegate is setup.
Reference to documentation
Core Application Design