Using the Proximity Sensor

suggest change
//Enabling the proximity Sensor
- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    [[UIDevice currentDevice] setProximityMonitoringEnabled:YES];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sensorStateMonitor:) name:UIDeviceProximityStateDidChangeNotification object:nil];
}

- (void)sensorStateMonitor:(NSNotificationCenter *)notification
{
    if ([[UIDevice currentDevice] proximityState] == YES)
    {
        NSLog(@"Device is close to user.");
    }

    else
    { 
        NSLog(@"Device is not close to user.");
    }
}

The selector receives an NSNotification, but the parameter is declared as NSNotificationCenter * (it is unused). The observer is not removed when the view disappears.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents