Follow the below mentioned step in order to implement flash light in iOS:

Step 1 Create Xcode Project

First of all create xCode project named it FlashLightDemo and saved it. This project will contain one View Controller, which will become main view controller.

Step 2 Add frameworks

To implement flash light we required AVFoundation framework. The AVFoundation framework is responsible for management and interaction with audio-visual media in iOS applications. AVFoundation framework needs to link to your application.

For that go to project, now select build Phases tab, and then go to Link Binary with Library, Add framework as following.

av foundation framework

Step 3 Design UI

Design User Interface describe as per the following screen.

user interface

Step 4 Code for Flashing

Write following function to the tap event of above mentioned button.

  • - (IBAction)btnFlashOnClicked:(id)sender
  • {
  • AVCaptureDevice *flashLight = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  • if ([flashLight isTorchAvailable] && [flashLight isTorchModeSupported:AVCaptureTorchModeOn])
  • {
  • BOOL success = [flashLight lockForConfiguration:nil];
  • if (success)
  • {
  • if ([flashLight isTorchActive])
  • {
  • [btnFlash setTitle:@"TURN ON" forState:UIControlStateNormal];
  • [flashLight setTorchMode:AVCaptureTorchModeOff];
  • }
  • else
  • {
  • [btnFlash setTitle:@"TURN OFF" forState:UIControlStateNormal];
  • [flashLight setTorchMode:AVCaptureTorchModeOn];
  • }
  • [flashLight unlockForConfiguration];
  • }
  • }
  • }

I hope you found this blog helpful while working Flashlight with one button. Let me know if you have any questions or concerns regarding iOS, please put a comment here and we will get back to you ASAP.

Leave a Reply