The proximity and ambient light sensors are two separate
sensors. The ambient light sensor is used to change the brightness
level of the device’s screen automatically, while the proximity sensor is
used by the device to turn the screen off when you put the phone to your
ear to make a call. Although it does have an ambient light sensor, the
iPod touch does not have a proximity sensor.
Unfortunately, there is no way to access the ambient light sensor in
the official SDK. However, developers can access the proximity sensor via
the UIDevice class. This sensor is an infrared LED emitter/detector pair
positioned near the earpiece, as shown in Figure 1. It measures the
return reflection of the transmitted infrared beam to detect (large)
objects near the phone.
You can enable the sensor in your application by toggling the
proximityMonitoringEnabled Boolean:
UIDevice *device = [UIDevice currentDevice];
device.proximityMonitoringEnabled = YES;
You can query whether the proximity sensor is close to the
user:
BOOL state = device.proximityState;
If proximity monitoring is enabled, a UIDeviceProximityStateDidChangeNotification notification will be posted by the UIDevice when the state of the proximity sensor
changes; you can ask that your application is notified when this occurs by
registering your class as an observer with the notification center:
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(proximityChanged:)
name:@"UIDeviceProximityStateDidChangeNotification" object:nil];
Notifications would then get received by the proximityChanged:
method:
- (void) proximityChanged: (NSNotification *)note {
UIDevice *device = [note object];
NSLog(@"In proximity: %i", device.proximityState);
}