Skip to main content

Attachments in Chat


A custom chat view controller used to show chat screen with image & file upload.


Add selectors to file and image button, on tap of file and image buttons we can open respective pickers like follows:

 @objc func addFileButtonTapped() {
var filePicker: UIDocumentPickerViewController
if #available(iOS 14.0, *) {
filePicker = UIDocumentPickerViewController(forOpeningContentTypes: [.pdf, .text, .plainText, .audio, .video, .movie, .image, .livePhoto], asCopy: false)
} else {
filePicker = UIDocumentPickerViewController(documentTypes: [], in: .import)
filePicker.delegate = self
present(filePicker, animated: true, completion: nil)

@objc func addImageButtonTapped() {
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
present(imagePicker, animated: true, completion: nil)

Implement delegates of pickerViews and once you have path to image/file this can be sent using selectedFileURL.path)