当应用程序打开时,一切都可以正常运行。但是一旦应用程序关闭,接近传感器也会停止。我到处检查过,这就是我所拥有的一切。
- (void)viewDidLoad
{
[super viewDidLoad];
//Initiates the proximity sensor
[[UIDevice currentDevice] setProximityMonitoringEnabled:YES];
[[UIDevice currentDevice] proximityState];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityStateDidChange) name:UIDeviceProximityStateDidChangeNotification object:nil];
//Background notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
}
-(void)applicationDidEnterBackground{
NSLog(@"Entered background state.");
[[UIDevice currentDevice] setProximityMonitoringEnabled:YES];
}
当应用关闭时,它会打印出“输入的背景状态”。但不会继续监控接近传感器。