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