/* 传感器的使用: 1.注册一个传感器通知; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stateChange:) name:@"UIDeviceProximityStateDidChangeNotification" object:nil]; 2,设置传感器的proximityMonitoringEnabled属性为YES; */- (void) toggle: (id) sender{ BOOL isIt = [UIDevice currentDevice].proximityMonitoringEnabled;//判断传感器是否是启动的 NSString *title = isIt ? @"Enable" : @"Disable"; self.navigationItem.rightBarButtonItem = BARBUTTON(title, @selector(toggle:)); [UIDevice currentDevice].proximityMonitoringEnabled = !isIt;//改变传感器状态 self.log = [NSMutableString string]; [self doLog:@"You have %@ the Proximity sensor.", isIt ? @"disabled" : @"enabled"]; if (!isIt) [self doLog:@"View state changes on the debugger consoler as the screen is not readable when blanked."];}- (void) stateChange: (NSNotification *) notification{ NSLog(@"notification.userinfo:%@",notification.userInfo); NSLog(@"The proximity sensor %@", [UIDevice currentDevice].proximityState ? @"will now blank the screen" : @"will now restore the screen");}- (void) viewDidLoad{ self.navigationController.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR; self.navigationItem.rightBarButtonItem = BARBUTTON(@"Enable", @selector(toggle:)); //注册传感器通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stateChange:) name:@"UIDeviceProximityStateDidChangeNotification" object:nil];}