The proximity sensor does not work on iPhone 4

//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]; } 

On iPhone 3GS or earlier proximityChanged: the method is called successfully, but on iPhone 4, while I find the object on top, the sensor (screen) is not detected. Any idea guys?

+6
source share
5 answers

There is nothing wrong with the code (assuming you really implemented proximityChanged: course). I checked your code on the iPhone 4 and it reacts to my hand, moving in front of the proximity sensor.

Perhaps the hardware is slightly different from 3GS, which means that it is more sensitive to what you are doing? Can you try another iPhone 4 device (or at least check that the proximity sensor works at all, for example, using the phone app)?

+1
source

I see several problems with this code. First you use

  name:@"UIDeviceProximityStateDidChangeNotification" 

instead

 name:UIDeviceProximityStateDidChangeNotification 

Both work, but using an empty version you will get a compiler error if you create a typo. (You want to get a compiler error with typos, this will prevent silent errors).

The next thing you do not actually verify is that the proximity sensor is available before adding a notification. Your code:

 BOOL state = device.proximityState 

But it just checks if the device is close to the face of the user. You really want to set proximityEnabled to YES , and then verify that it is actually installed. This is a bit controversial.

 UIDevice *device = [UIDevice currentDevice]; [device setProximityMonitoringEnabled:YES]; if ([device isProximityMonitoringEnabled]) { // Do your stuff } 

Here is an example of the complete code:

 NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; UIDevice *device = [UIDevice currentDevice]; // Register for proximity notifications [device setProximityMonitoringEnabled:YES]; if ([device isProximityMonitoringEnabled]) { [notificationCenter addObserver:self selector:@selector(proximityChanged:) name:UIDeviceProximityStateDidChangeNotification object:device]; } else { NSLog(@"No Proximity Sensor"); } 
+2
source

Apple Docs: "Not all iOS devices have proximity sensors. To determine if proximity monitoring is available, try turning it on. If the value of the proximityMonitoringEnabled property is set to NO, proximity monitoring is not available."

+2
source

You should always check if a particular device has a proximity sensor or not. Not all iOS devices have proximity sensors.

 BOOL state = device.proximityState; if(state) [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityChanged:) name:@"UIDeviceProximityStateDidChangeNotification" object:nil]; else NSLog(@"NO"); 
0
source

Source: https://habr.com/ru/post/911396/


All Articles