- (void)viewDidLoad { [super viewDidLoad]; // 默认情况下传感器处于关闭状态 [UIDevice currentDevice].proximityMonitoringEnabled = YES; // 添加到通知中心,状态改变调用方法 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityStateDidChange) name:UIDeviceProximityStateDidChangeNotification object:nil]; } - (void)proximityStateDidChange { // 用距离传感器判断是否有东西靠近还是离开 if ([UIDevice currentDevice].proximityState) { NSLog(@"有东西靠近"); } else { NSLog(@"有东西离开"); } }