/* 傳感器的使用: 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]; }