Search code examples
iphoneiosobjective-cproximity

Proximity sensor on when hand move around Sensor


I am developing one app in that i want to get the proximity sensor on when user move hand around proximity sensor and get proximity state value like 0. when user move hand second time value get 1 how may i do this i know how to enable proximity but i want to enable proximity when user move hand around proxy. so how may i do this i tried this code. to enable proximity.

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

    if (device.proximityMonitoringEnabled)
    {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(handleProximityChange:)
                                                     name:UIDeviceProximityStateDidChangeNotification
                                         object:nil];
    }
    else
    {
        NSLog(@"Device not capable to support Proximity sensor");
        // device not capable
    }

and catch the proximity state in this method...

-(void)handleProximityChange:(NSNotification *)notification
{

    NSLog(@"Proximity event catch");

}

i will do this code as per user hnd over in proximit so hoe may i do this please help me to do this.


Solution

  • - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        count = 0;
        condition = 0;
        [self addProximitySensorControl];
    }
    
    -(void)addProximitySensorControl {
    
        UIDevice *device = [UIDevice currentDevice];
        device.proximityMonitoringEnabled = YES;
    
        BOOL state = device.proximityState;
        if(state)
        {
            NSLog(@"YES");
            _sensorSupport.text = @"YES";
        }
        else
        {
            NSLog(@"NO");
         _sensorSupport.text = @"No";
        }
    
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(proximityChanged:)
                                                     name:@"UIDeviceProximityStateDidChangeNotification"
                                                   object:nil];
    }
    -(void)proximityChanged:(NSString*)str
    {
        NSLog(@"i am in proximityChanged");
        condition++;
        if (condition % 2 == 0) {
            count++;
        }
        _counter.text = [NSString stringWithFormat:@"%d",count];
    }