0

I'm working on a feature that shows a preview of the files before sending them. Basically, The user selects the iPhone's Files application from within the application and sees the preview after selecting the file he wants to send. Then it sends. So far I was able to do this. However, if the selected files' size are 200 MB or more, I show a warning. But I couldn't do that. I want to calculate this while "selecting files" -before the preview is shown. How can I do it?

I defined the documents selecting class. In this class, I created the document select class and defined all file types.

import UIKit
import MobileCoreServices

final class AttachmentDocumentBuilder {
    static func make() -> UIDocumentPickerViewController {
       let vc = UIDocumentPickerViewController(documentTypes: [String(kUTTypeText),
                                                               String(kUTTypeContent),
                                                               String(kUTTypeItem),
                                                               String(kUTTypeData)], in: .import)
        vc.allowsMultipleSelection = true
        vc.modalPresentationStyle = .fullScreen
        return vc
    }
}

And here, I wrote an extension that shows the preview of a document. The "totalSize" variable checks the document size but the warning appears after opening the preview and pressing the submit button.

extension ShareAttachmentRouter: QLPreviewControllerDelegate {
    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        let item = DocumentItem(url: urls)
        let totalSize = item.url.compactMap { try? FileManager.default.attributesOfItem(atPath: $0.path) }.compactMap { $0[FileAttributeKey.size] as? Int }.reduce(0, +)
        guard totalSize < 200 else {
            UIApplication.topViewController()?.showMessage(AppMessage.documentSizeLimitExceeded(limit: 200,
                                                                                                actions: [UIAlertAction(title: String.localized("OK"),
                                                                                                                        style: .default,
                                                                                                                        handler: { (_) in UIApplication.topViewController()?.becomeFirstResponder()
            })]))
            return
        }
        
        let vc = AttachmentDocumentPreviewBuilder.make(item, chatId: chatId, documentsURL: item.url)
        vc.modalPresentationStyle = .formSheet
        vc.delegate = self
        changePresentedVC(vc)
    }
}

  • I think you're going to have to implement your own view controller for selecting documents. Alternatively, you may be able to find a library/pod you can. – Rob C Apr 11 '23 at 00:00

0 Answers0