Let there be light etc.

Dining at a dark destination got you down? Can’t see your menu and don’t want to blind neighbors with the harsh truth that is the full brightness of your iPhone flashlight? Here is a tutorial for creating utilizing the flashlight and adding a dimmer.

Create a new single view application XCode file, and import AVFoundation into the ViewController.swift file.

Add a UISlider and a UISwitch into the ViewController and set up the constraints as you wish. (Make sure your switch is in the “off” state to begin.) Then declare the IBOutlets in your ViewController.swift file. I named my UISwitch: lightSwitch and my UISlider: brightnessSlider.

Next, in the viewDidLoad function. Set your UISlider’s minimum and maximum values as 0.0–1.0 respectively. Now, add a target to your UISlider with an action as a ‘Selector’ that you will create in the next step. The control event is checking to see if the slider’s value has changed. Lastly, disable the UISlider to begin with (this way if the flashlight is off you can’t change the brightness setting when the project is completed.

You are now done messing around with the viewDidLoad function and are ready to create your own functions.

Your first function will control the toggling of the light between on and off. It has no parameters and no return value. Declare a constant “device” equal to AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo). Next, check to see if your device has a flashlight by checking for a torch (accessed as a get only boolean using “hasTorch”). The next chunk of code has some error handling to take care of so you will have a couple “do-try-catch” sections.

You have the ability to “lockForConfiguration()” with the intention of gaining access to the camera’s settings like focus. Since the device constant is a device with media type, it is necessary to lock and unlock for configuration.

You need to check if the device’s torch mode is on and if it is, set it to off using “AVCaptureTorchMode.Off” and then disable the UISlider. If it is not on then you must try (error handling) to set the torch mode to a level between 0.0 and 1.0, and then enable the UISlider.

Now, create an IBAction for your UISwitch and call your toggle function inside of the action function.

At this point you should test your switch using a real device to run the application as the simulator does not have a flash to test.

Once you know that the toggle is working correctly you can move on to changing the brightness. Remember the function that you named in the viewDidLoad() as a selector. I called mine “sliderValueChanged.” Make sure you use the same name you declared as your selector!

This function is an IBAction with your UISlider! as a parameter. Begin the function the same way you did the toggling function with your constant device. Now, create a constant that will represent the current value of your UISlider making sure to have it be type Float. The next portion is the same as the error handling from the toggle switch without the on and off conditionals. This time, when setting the torch mode level, set it equal to your current value constant. It should look like the following snippet when you are done.

If you have any questions about the code, or notice any errors, please comment below. Enjoy your new tool!