Keep display on when the proximity sensor is covered

后端 未结 5 1019
隐瞒了意图╮
隐瞒了意图╮ 2021-02-05 12:39

I want to intercept the proximity sensor without turning off the display.

I know by the documentation that I have two Bool variables:

proximityMonitoring         


        
5条回答
  •  迷失自我
    2021-02-05 13:24

    Apple’s documentation notes that “Not all iPhone OS devices have proximity sensors.” To determine if the device your app is running supports proximity monitoring, set the proximityMonitoringEnabled property to YES, then check its value:

    UIDevice *device = [UIDevice currentDevice];
    [device setProximityMonitoringEnabled:YES];
    
    if (device.proximityMonitoringEnabled == YES) {
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(proximityChanged:) 
                                                     name:@"UIDeviceProximityStateDidChangeNotification"
                                                   object:device];
    }
    
    - (void) proximityChanged:(NSNotification *)notification {
        UIDevice *device = [notification object];
        NSLog(@"In proximity: %i", device.proximityState);
    }
    

    Source: http://www.whatsoniphone.com/blog/new-in-iphone-30-tutorial-series-part-4-proximity-detection/

    Will help to detect current state of sensor.

    Public API that allows screen dim:

    [UIScreen mainScreen].wantsSoftwareDimming = YES;
    [UIScreen mainScreen].brightness = $your_brightness_value;
    

    Found here: Change to wantsSoftwareDimming in iOS 6?

提交回复
热议问题