Search code examples
iphoneiosproximitysensor

Proximity Sensor is not working in iPhone 4 device


//I have created below snippet to let the sensor to be detected.

-(void)addProximitySensorControl {

    UIDevice *device = [UIDevice currentDevice];
    device.proximityMonitoringEnabled = YES;

    BOOL state = device.proximityState;
    if(state)
        NSLog(@"YES");
    else
        NSLog(@"NO");

    [[NSNotificationCenter defaultCenter] addObserver:self
                            selector:@selector(proximityChanged:)
                                name:@"UIDeviceProximityStateDidChangeNotification"
                                object:nil];
}

In the iPhone 3GS or earlier proximityChanged: method is called successfully but in iPhone 4 while I am hovering object from upwards the sensor(screen) its not being detected. Any idea Guys?


Solution

  • There is nothing wrong with your code (assuming that you did implement proximityChanged: of course). I tested your code on an iPhone 4 and it responds to my hand moving in front of the proximity sensor.

    Maybe the hardware is slightly different on the 3GS, meaning it is more sensitive to what you are doing? Can you try on a different iPhone 4 device (or at least verify that the proximity sensor works at all e.g. by using the phone app)?