FUIDocumentPickerAttachmentAction

public class FUIDocumentPickerAttachmentAction : NSObject, FUIAttachmentAction

This class is an implementation of FUIAttachmentAction to allow user to choose a file using UIDocumentPickerViewController as an attachment in FUIAttachmentsFormView.

If the application wants to have its Document directory accessible by the UIDocumentPickerViewController, the application needs to have the following 2 properties set to true in its plist:

  • “Supports opening documents in place” (LSSupportsOpeningDocumentsInPlace)
  • “Application supports iTunes file sharing” (UIFileSharingEnabled)

Note that the file URL returned by the document picker is a temporary url. The content of the file may not be available after the document picker is closed. Therefore, developer should copy the file content to a location that it may attach the file content later.


class TestFormTableViewController: FUIFormTableViewController {
    override func viewDidLoad() {
        // ...

        // prepare temporary directory to hold attachment files
    }

    // ...

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // ...

        let cell = tableView.dequeueReusableCell(withIdentifier: FUIAttachmentsFormCell.reuseIdentifier, for: indexPath) as! FUIAttachmentsFormCell
        cell.attachmentsController.delegate = self
        cell.attachmentsController.dataSource = self
        cell.attachmentsController.reloadData()

        let documentPickerAction = FUIDocumentPickerAttachmentAction()
        documentPickerAction.delegate = self
        cell.attachmentsController.addAttachmentAction(documentPickerAction)

        return cell
    }
}

extension TestFormTableViewController: FUIDocumentPickerAttachmentActionDelegate {
    var documentPicker: UIDocumentPickerViewController {
      return UIDocumentPick  erViewController(documentTypes: ["public.data"], in: .import)
    }

    func documentPickerAttachmentAction(_ action: FUIDocumentPickerAttachmentAction, didPickFileAt url: URL) {
        if let savedUrl = saveFileToTempFolder(url) {
            self.addAttachmentURL(savedUrl)
        }
        self.tableView.reloadSections(IndexSet(integer:self.attachmentSection), with: .automatic)
   }
}

Attention

The delegate object with type FUIDocumentPickerAttachmentActionDelegate is declared as a weak reference. On deallocation it will be automatically set to nil. To keep it alive as expected, developer should retain the delegate object during its whole execution scope.