This framework provides an easy way to handle camera, phone library and video pickers. It handles permissions and returns a picked data in a delegate.
0. Import module
1. Add Privacy Usage Description in your Info.plist for intended pickers you are going to use. - Important!
2. Subclass MediaPickerPresenter and conform to protocol:
class ViewController: UIViewController, MediaPickerPresenter {
var attachmentManager: AttachmentManager = AttachmentManager()
//...
func didSelectFromMediaPicker(_ file: FileInfo) {
//do more with file...
}
3. (Optional) Customize AttachmentManager:
override func viewDidLoad() {
super.viewDidLoad()
//For example:
var titles = attachmentManager.settings.titles
var settings = attachmentManager.settings
//Customize titles
titles.actionSheetTitle = "My title"
titles.cancelTitle = "CANCEL"
//Customize pickers settings
settings.allowedAttachments = [.photoLibrary, .documents];
settings.documentTypes = ["public.image", "public.data"];
settings.libraryAllowsEditing = true
settings.cameraAllowsEditing = true
}
4. Open menu:
presentAttachmentActionSheet()
5. Check the result in delegate:
func didSelectFromMediaPicker(_ file: FileInfo) {
print("Picked file: \(file.fileName)")
//do more with file...
}
Feel free to comment and share your thoughts.
There are sources that were very helpful for creating the framework:
- https://gist.github.com/rpassis/4622291029cd12e4ce2b7585d3e62d15
- https://medium.com/@deepakrajmurugesan/swift-access-ios-camera-photo-library-video-and-file-from-user-device-6a7fd66beca2
The MIT License (MIT)
Piotr Błachewicz 2018