A copy — paste solution done in Swift Accessing Camera and Photo Library is maybe one of the most common features that you can find in almost every app that we build. That’s why we have to make sure that we do it correctly and that we have a custom class ready to be reused at any time. iOS In this tutorial, I will show you how to create the custom class in , and have it by your hand whenever you need it. If you are lazy to read the whole tutorial, you have a GIST file at the bottom of this post that you can download. I will name the class Swift CameraHandler.swift. Access iOS Camera and Photo Library I will start by creating two functions, the first will be named camera() and the second one photoLibrary(). We are using the class for both cases, and all we need to do is to change the property to the suitable one. UIImagePickerController sourceType camera() func camera() { if UIImagePickerController.isSourceTypeAvailable(.camera){ let myPickerController = UIImagePickerController() myPickerController.delegate = self; myPickerController.sourceType = .camera currentVC.present(myPickerController, animated: true, completion: nil) } } photoLibrary() func photoLibrary() { if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){ let myPickerController = UIImagePickerController() myPickerController.delegate = self; myPickerController.sourceType = .photoLibrary currentVC.present(myPickerController, animated: true, completion: nil) } } As you can see, the sourceType changes to and types. We will need the and so we intercept the image that is picked by the user. I will explain below about the property. .camera .photoLibrary UIImagePickerControllerDelegate UINavigationControllerDelegate currentVC Create UIActionSheet Next, what we are going to do is create a function that will present both options to the user in a simple . UIActionSheet showActionSheet() func showActionSheet(vc: UIViewController) { currentVC = vc let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (alert:UIAlertAction!) -> Void in self.camera() })) actionSheet.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { (alert:UIAlertAction!) -> Void in self.photoLibrary() })) actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) vc.present(actionSheet, animated: true, completion: nil) } This is the only function that you will need to call in order to show the iOS Camera and Photo Library. It is showing a simple UIActionSheet with the both options available. Also, we are passing a parameter called that will be passed to the private property. We are doing this in order to handle the presentation of the controllers directly from inside the class. vc currentVC NOTE: If the device doesn’t supports camera or photo library, nothing will happen when you press an option. For example, testing the camera feature on simulator. Delegate methods At the end of the file in we will create an , and we will call the delegate methods and that belongs to the UIImagePickerControllerDelegate. extension didFinishPickingMediaInfo imagePickerControllerDidCancel extension CameraHandler: UIImagePickerControllerDelegate, UINavigationControllerDelegate{ func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { currentVC.dismiss(animated: true, completion: nil) } func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { if let image = info[UIImagePickerControllerOriginalImage] as? UIImage { self.imagePickedBlock?(image) }else{ print("Something went wrong") } currentVC.dismiss(animated: true, completion: nil) }} We are now able to get the picked image from the photo library or the captured one from the camera. To make things simpler and clearer, I have created a closure named that will provide us with the picked image where we need it. Here are all the properties that you will need. imagePickedBlock() static let shared = CameraHandler() fileprivate var currentVC: UIViewController! //MARK: Internal Propertiesvar imagePickedBlock: ((UIImage) -> Void)? How to use? We are done creating the class now we need to use it. The beauty in classes like this one is the easy reuse. CameraHandler.shared.showActionSheet(vc: self)CameraHandler.shared.imagePickedBlock = { (image) in /* get your image here */} That’s it from this tutorial that showed you how to access iOS Camera and Photo Library in Swift 3, and I really hope that it helped you. Please do share this post as a support or comment in the comment section for any questions that you might have. Complete GIST file That’s it from this tutorial and if it helped you please 👏 or share this story so others like you can find it. Thank you for your attention! 🚀 Check out my latest project: _HOT ODDS Each day, we generate a list of the hottest odds in the world. These are odds that have dropped the most…_apple.co 1x2 BET - Soccer Tips & Odds Read more of my writing on Medium: _Forget MVC, now!_hackernoon.com Introducing Clean Swift Architecture (VIP) _Many iOS apps use Google Maps. This is a very common feature, so I have decided to prepare an ultimate guide on the…_medium.freecodecamp.org Your ultimate guide to the Google Maps SDK on iOS, using Swift 4 _Custom UIView with XIB file is a very common practice in iOS Development. Custom UIView classes don’t contain XIB files…_medium.com SWIFT — Custom UIView with XIB file _A Swift tutorial that will make your app available in Spotlight search_hackernoon.com How to add Spotlight support to your iOS app _Understanding One-to-One and One-To-Many relationships_hackernoon.com Core Data Relationships _All you need to know about Auto Layout_hackernoon.com Understanding Auto Layout in Xcode 9 Subscribe to my Newsletter: