How can you turn on the led light on the iphone 4?

marty picture marty · Jul 1, 2010 · Viewed 12.3k times · Source

Possible Duplicate:
Turn on torch/flash on iPhone 4

I just want to be able to turn on the led light. Is there a simple way to do this, or am I going to need to, say, set up the phone to take a video, simulate it videoing with the light on, but not save the video? Something like that? Thanks.

Answer

Tibidabo picture Tibidabo · Oct 23, 2012

Try this, it worked fine for me.

#import <AVFoundation/AVFoundation.h>


- (void) turnTorchOn: (bool) on {

  Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
  if (captureDeviceClass != nil) {
  AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  if ([device hasTorch] && [device hasFlash]){
    [device lockForConfiguration:nil];
    if (on) {
        [device setTorchMode:AVCaptureTorchModeOn];
        [device setFlashMode:AVCaptureFlashModeOn];
        torchIsOn = YES;
    } else {
        [device setTorchMode:AVCaptureTorchModeOff];
        [device setFlashMode:AVCaptureFlashModeOff];
        torchIsOn = NO;            
    }
    [device unlockForConfiguration];
    }
  }
}