// 1開啟距離感應功能
[UIDevice currentDevice].proximityMonitoringEnabled = YES;
// 2監(jiān)聽距離感應的通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(proximityChange:)
name:UIDeviceProximityStateDidChangeNotification
object:nil];
- (void)proximityChange:(NSNotificationCenter *)notification {
if ([UIDevice currentDevice].proximityState == YES) {
NSLog(@"某個物體靠近了設(shè)備屏幕"); // 屏幕會自動鎖住
} else {
NSLog(@"某個物體遠離了設(shè)備屏幕"); // 屏幕會自動解鎖
}
}