Join David Okun for an in-depth discussion in this video Implementing your framework delegate, part of Swift: Writing Reusable Frameworks.
- [Instructor] Let's go ahead and implement the delegate that we've created in our framework inside of our sample application. Go to the ViewController inside of your sample application. And go underneath the line where you create your camera, on line 16. Type "camera.delegate = self". At this point, if you try to build your application, it should fail on you, because it will say, "Cannot assign value of type 'ViewController' "to type 'CameraControllerDelegate'". We don't want to automatically fix this, because we want to use an extension to organize all the code that has to do with our delegate.
So close this, and scroll to the bottom of your ViewController. Outside the declaration of your base class, type "extension ViewController" of type "CameraControllerDelegate". This is where you can specify all the functions where your ViewController must adhere to the protocol CameraControllerDelegate. But you'll notice that it still gives you an error. Let's click this and read it. "Type 'ViewController' does not conform "to protocol 'CameraControllerDelegate'". The nice thing in Xcode 9 is that it will add the protocol stubs for you automatically. So we can click Fix here, and you'll notice that it auto-completes the code for you.
So you have a nice easy place to put any logic that happens whenever your application receives a message that the cancel button was tapped. What we're going to do here, is we're going to choose to dismiss the controller whenever the cancel button is tapped. However, to show you that you can do whatever you want whenever this occurs, we're going to put a breakpoint to show you whenever this is triggered. Here we'll type "controller.dismiss", animated should be "true", and completion should be "nil". So that all we're saying is we want to dismiss the ViewController that is the controller, we'll put a breakpoint here to show what happens whenever that button is actually tapped.
Let's build it to make sure that our framework is compiling as well as our sample application. Now we're going to go ahead and run the camera app to show what the UI looks like and what happens when you tap the cancel button. You'll notice at the bottom left-hand corner of our UI we've now got a nice shiny cancel button. Let's go ahead and tap that button. If you go to Xcode, you'll notice that our breakpoint has been triggered, showing that we've sent a message back to our sample application that the cancel button was tapped. In this case, it will run our logic of dismissing the controller, but it's important to point out once again that implementing the delegate in this way gives you the opportunity to do whatever you want when this action or any action you specify in the future occurs.
If we let it go ahead and run, you'll notice that it disappears. It reappears because we've said it in view will appear, but that's expected behavior.
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