How do I support Files app integration in Swift?

To support Files app integration in Swift, you can leverage the Document Picker to allow users to pick files from the Files app. This is particularly useful for applications that need to open, save, or import various file types. Below is a simple example demonstrating how to implement this.

// Import necessary framework import UIKit class ViewController: UIViewController, UIDocumentPickerDelegate { override func viewDidLoad() { super.viewDidLoad() // Example button to trigger document picker let button = UIButton(type: .system) button.setTitle("Open Document", for: .normal) button.addTarget(self, action: #selector(openDocument), for: .touchUpInside) button.center = view.center view.addSubview(button) } @objc func openDocument() { let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.data"], in: .import) documentPicker.delegate = self documentPicker.modalPresentationStyle = .formSheet present(documentPicker, animated: true, completion: nil) } func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) { // Handle the selected file if let selectedFileURL = urls.first { // Process the file print("Picked file: \(selectedFileURL.absoluteString)") } } }

Keywords: Swift Files app integration UIDocumentPicker Document Picker File handling