iOS距离传感器的简单使用

// 打开距离传感器
[UIDevice currentDevice].proximityMonitoringEnabled = YES;

// 通过通知监听有物品靠近还是离开
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityStateDidChange:) name:UIDeviceProximityStateDidChangeNotification object:nil];

- (void)proximityStateDidChange:(NSNotification *)note
{
    if ([UIDevice currentDevice].proximityState) {
        NSLog(@"有东西靠近");
    } else {
        NSLog(@"有东西离开");
    }
}

你可能感兴趣的:(iOS距离传感器的简单使用)