Any application that uses a framework is going to need to be notified when things happen. As you make your camera framework, you need to set up a delegate so that the application can be aware of events that the camera performs. The best part of this is that you likely already know how to do it.
- [Narrator] Now let's talk about how to handle events that occur inside of your framework. If you've worked with IOS Development to this point, it's very likely that you've encountered the use of a delegate, which gives you the functionality to call back to one object from another. We're going to follow the same kind of logic here by implementing a delegate that's inside of your camera controller to call back to the application using your framework. Go to the line that says import AV Foundation. Hit return, and underneath it type public protocol, camera, controller, delegate.
You'll notice that we specified it to be public so that the application that uses this framework has access to this protocol. We're going to type one function called func, cancel, button, tapped, and the parameter will pass in is the controller on which the cancel button was tapped. Think about the use case here. When we're dealing with a cancel button being tapped, you need to make sure that that's a button occurs on the UI, and you want to have custom logic from whenever that button is tapped, so that the user implementing this framework can decide what to do with it after the cancel button is tapped.
In this case, we need to first make a cancel button. Scroll into your declaration of camera view controller, and between preview layer and position, go ahead and create a button. We'll do it this way so that we have a easy way to add it as a subview in other parts of our view controller. We'll type a private bar first, and then we'll type our short property to be internal. Remember that we're making this internal because we don't want the user to have direct access to this button to manipulate it, but we do want it to be able to call back via our public delegate.
Make sure it's of type UI button, and inside your accessor, first we want to say if let current button is in our private variable, which means that we've already created something. We simply want to return what we've already created. If we haven't created it, then we want to go ahead and do so. It's messy and it's generally not good practice to decide the frame inside the constructor of a button, especially if you're doing it in code. We're going to do it now and refactor it later for part of another video in this course. So specify the frame for here, and you want to type CG Rect, open it with a constructor, and specify your frame.
After you've specified the frame of your button, we want to set a title for it, so we do button, dot, set, title. We'll choose to say cancel, and UI control stage should be normal. Furthermore, we're going to set a target so that whenever the button is tapped, we know what method to access. So we type button, dot, add, target. The target should be self, because it's inside this view controller, and for now we're going to type hash, selector, and then open to say cancel, button, tapped. If you're thinking, wait, we haven't named this function yet, you're right, and we're going to name it as soon as we finish typing this button logic out.
The control event we want to specify is dot, touch, up, inside. Now we want to make sure that we assign this button to our private variable that we've created earlier, and then we return the button. Now that we've created it in this way, the last thing we need to do, is we need to make sure that we're adding it to our subview and our UI. We can scroll down to the create UI function we've written earlier, and underneath the adding of the sublayer of the preview layer, we type self, dot, view, dot, add, subview, and we'll type self, dot, cancel button.
Now we've added the button to our UI. But in order to make this work, we now need to make sure that we're adding the function that will occur whenever the button is tapped. Scroll to the bottom of your file, and make another file private extension on camera view controller. Before you type the function, make sure that you mark this so that it says mark, UI button, functions. Then you want to type func, cancel, button, tapped. Don't pass any parameters and open this. At this point, try to build your framework and you'll notice that it gives you an error.
Scroll up to where the error is inside of your cancel button method, and click what it says. It will notify you that in order to make this work, you need to add add objective C to expose this instance method to objective C. What's actually happening here is part of another topic in swift, so we won't cover that for that in this course, but we'll just go ahead and click fix here. If you scroll down, you'll notice that it's added at objective C to the beginning of our function signature. Down here, we now want to say whatever happens whenever we tap the cancel button. So we now need to add our delegate inside of our view controller so that we have a handle on it, so that we can send a message through it.
Scroll back up to the logic where you wrote your cancel button before. Between the end of your button declaration and the position of your camera object, go ahead and type open, bar, delegate, and specify that it is of type camera, controller, delegate. Mark it as optional with a question mark. The reason we're doing this is because we only want to make use of the delegate and its functions, if the application implementing this framework has specified that there is a delegate. Additionally, we notice up here that we accidentally wrote cancel button instead of current button.
Click this and correct this typo. The warning should go away shortly. Now that we've created our delegate, we want to scroll down to the cancel button tapped function that we've implemented. Let's start by typing self, dot, delegate, dot, cancel button tapped, controller, self. You'll notice that it automatically filled in a question mark for us after the delegate, though this will work in most cases, this is dangerous code to write, and we want to protect ourselves against exceptions. Because we've marked our delegate as optional, it may evaluate to nil.
If we do this, we may be running a function against the nil value and unwrap a nil value. This means that we need to make sure we have something assigned to our delegate before we try to run anything. Good practice dictates that we should do this instead. Type if, let, delegate equals self, dot, delegate, open curly brackets, and inside those curly brackets, type delegate, dot, cancel button tapped, with controller self. Delete the line below it. Just make sure that if you have a delegate assigned with your framework, only then will you run this function.
Now that you've implemented this delegate, go ahead and build your framework to make sure everything compiles as expected. Once you see that it does, you're now ready to head back over to your sample application and implement your delegate to get your callback whenever you click the cancel button.
Along the way, he explains the differences and nuances between writing code for an application and for a reusable framework, as well as some of the fundamentals of AVFoundation, one of the core camera frameworks in iOS. David also shows how to refactor your code, understand Swift access control, develop an interface, and handle memory leaks, so your framework is ready to share with other developers.
- Creating your first build
- Making the camera work
- Creating a framework delegate
- Adding media
- Capturing images
- Correcting orientation
- Versioning and tagging releases in Git