Use the device's flash unit on iOS

  • Using the device's flash during an AR session can enhance visibility in low-light conditions.

  • Before enabling the flash, verify if the active camera configuration supports it by checking the hasTorch property of the AVCaptureDevice.

  • Enable the flash by setting the torchMode property of the AVCaptureDevice to AVCaptureTorchModeOn.

  • Disable the flash by setting the torchMode property of the AVCaptureDevice to AVCaptureTorchModeOff.

Enabling the device's flash unit during an AR session can help improve visibility.

Check that the current camera configuration supports flash

Not all camera configurations support enabling a flash unit. Before enabling the flash or offering users the option to enable the flash, ensure that the flash unit is available for the active camera configuration:

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (device) {
    return device.hasTorch;
}
return false;

Enable the flash unit

Enable the flash unit by configuring the AR session with AVCaptureTorchModeOn:

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (device) {
    if (device.hasTorch) {
        NSError *error = nil;
        if ([device lockForConfiguration:&error]) {
            device.torchMode = AVCaptureTorchModeOn;
            [device unlockForConfiguration];
        } else {
            return;
        }
    }
}

Disable the flash unit

Disable the flash unit by configuring the AR session with AVCaptureTorchModeOff:

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (device) {
    if (device.hasTorch) {
        NSError *error = nil;
        if ([device lockForConfiguration:&error]) {
            device.torchMode = AVCaptureTorchModeOff;
            [device unlockForConfiguration];
        } else {
            return;
        }
    }
}