Frage

//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?

War es hilfreich?

Lösung

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)?

Andere Tipps

I can see a few problems with this code. The first is that you use

 name:@"UIDeviceProximityStateDidChangeNotification"

instead of

name:UIDeviceProximityStateDidChangeNotification

Both work, but using the bare version will give you a compiler error if you make a typo. (You want to get a compiler error with typos, it prevents silent errors).

The next thing is that you aren't actually checking for the proximity sensor being available before adding the notification. Your code:

BOOL state = device.proximityState

But this just checks whether or not the device is close to the users face. What you really want is to set proximityEnabled to YES, then check that it actually got set. It's a little counterintuitive.

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

Here's a full code sample:

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");
}

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

You should always check whether the particular device have 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");
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top