Everybody this is Caleb with Dev slopes and in this video it's going to be a shorter one we're going
to basically enable our camera to utilize the flash.
We can set it to be automatic but I think it's cool to give the user the ability to control whether
or not their flash is on or off.
So we're going to do that.
It's very easy.
Let's go ahead and pull open that X code project and let's make it happen right now.
So to begin we actually need to think about the flash option.
We either can have the flash on or off and to control that we're going to use an enum to just kind of
control the state of the flash the flash light.
So an enum can be used for this.
We can go and Hedden type ENM flash state and we can give it to cases either off or on Cayne we can
use this ENM to basically save the state of the on or off light and then later we can tell the camera
that we want it to be off or on based on what the user sets with the buttons.
OK very cool.
And actually before we even go further than this we're going to need to create a default state.
So when the app opens we're going to set it to be off by default because I think most people would probably
want it to be off unless they want it to be on for the most part.
So to do that.
Go ahead and type var flash control state.
That's of course going to be of type flash state and we're going to actually just do this.
We're going to set it to be off by default.
Now when this variable is utilized when we actually instantiated it's going to get set to be off just
from the get go.
OK that's very cool.
Very easy way to do it.
And you know while I'm here I'm going to get rid of that comment.
I don't need it.
And now what we're going to do is we're going to utilize that ENM to go ahead and set up our settings
for the flash and the way we do that is where we capture our photo.
Did tap camera view.
OK.
So we have capture photo here.
And the cool thing is one of the settings is the flash mode.
So we're going to use our our variable appear flash control state which of course is set to off at the
beginning.
We're going to go ahead and we're going to type if Flash control state is equal to off.
OK so when we open the app if it's equal to off we're going to call settings flash mode equals off else
telling you guys this is easy stuff else settings.
Flash mode equals on.
And actually you know what you probably noticed there is an auto setting which automatically monitors
the light around you but the user wants to have control ultimately over the flash mode whether it's
on or off.
OK.
So that's that if it's off the flash mode will be off.
If it's on the flash motile be on.
All right.
Super easy now that's how we're going to control it.
But the thing we need to think about is how will we change the flash control state and we're going to
use.
Let me show you the example here where is it.
We're going to use the flash button to change the state.
If it's off and we tap the button it should change to say on and we should change the flash control
state to be on as well.
So to do that we're going to use an I.B. action as you may have suspected.
So to do that go ahead and go to the main storyboard let it load.
There we go.
And go ahead and tap on the little assistant editor and it looks like we're already properly in our
control or that's great.
So scroll down to the bottom Iby actions are usually put at the bottom of a code file just by convention
and go ahead and right click and then drag and we're going to use this to create an IB action.
So go ahead and type choose action then type flash button was pressed to basically describe what happens
when that IAB action is used.
We're pushing a button to trigger that action.
Now what we're going to do is we're basically going to use a switch to flip between the two different
states.
So go ahead and type switch and we're going to create a switch of flash control state and there are
two cases of course that we can switch between one is case off and the other one is case on.
Simple as that.
Now if the case are sorry if the flash control state is off and we tap the button we should change the
button title to say on OK if it's off and we tap it it should say on if it's on and we tap it it should
stay off.
So to do that go ahead and type flash control or flash button I suppose.
And to change the title you need to do set title.
Now of course the title string will say whoops we'll say flash on in caps and the UI control state is
actually just normal.
That way we can actually set it when we tap the button.
And after that if if the flash control state is off we tap the button to make it say on.
We should also change the flash control state to be on because then the flash should be on.
OK.
Simple as that.
And you're probably guessing this already but basically we're going to do the exact opposite of this
for the other case so I'm actually going to copy and paste it because it's very very very little code.
I'm going to change the title to say flash off and the control state will also be off.
Now believe it or not this is all we needed to do to set up our flash but there's one thing that's just
been bugging me.
This green view I want to change it to be white again because it shouldn't be shouldn't be green just
doesn't look right.
Kate go hadn't changed that to white and build and run this out.
Let's go see how we did.
Going to close.
My assistant editor there.
It should build and run.
We should see it.
And we should be able to turn the flash off.
OK.
Let's try to take a picture here.
OK good.
In a flash.
Let's tap the button.
The label changed and let's see when I take a picture now.
Hey there's the flash.
It works.
That's so cool.
So let's try it again.
Yep we got a flash.
Let's turn it off know flash see that super super cool awesome job guys that's all we needed to do to
integrate the flash in the next video we're actually going to go ahead and we're going to set up A-V
speech synthesizer so that we can speak what is happening what are what our phone is seeing right now
it's seeing a remote control.
We're going to set it up so that it says I think this is a remote control and I'm 99 percent sure if
it's not sure it's going to say this message.
I'm not sure what this is.
Please try again.
Simple as that.
So we'll see in the next video.
Amazing work with this one.
This is Caleb with Dev slopes dot com.