SWIFT / AI ENGINEERING ————– Three Files, One Task: Building a Document Agent with SwiftUI

A practical design for iPhone and iPad apps that turn selected documents into useful answers.

A consultant opens three proposals on an iPad and asks an assistant to compare delivery dates, exclusions, and payment terms. The useful result is a concise brief with supporting passages. Getting there requires more than connecting a chat interface to a model: the app must manage document access, extraction, task state, and user control.

For a mobility team working in Swift, this is an opportunity to build intelligence directly into a familiar document workflow. Let the user choose the files, keep processing visible, and make every action accountable to the current task.

Start with the mobile permission model

Full Disk Access belongs to macOS. An iPhone or iPad document assistant should be designed around its app sandbox and user-selected documents, rather than a desktop permission setting.

SwiftUI’s fileImporter presents the system document picker. Restrict supported content types with UTType, enable multiple selection when appropriate, and show the selected files before processing. Documents may come from local storage, iCloud Drive, or another file provider, so selection does not guarantee immediate availability.

For security-scoped URLs returned by the importer, call startAccessingSecurityScopedResource() before access and balance each successful call with stopAccessingSecurityScopedResource() when finished. Handle failures without silently broadening the task.

Selecting a document authorizes a file workflow; uploading its contents requires a separate, clearly explained processing decision.

Give the SwiftUI screen a clear state

Model the workflow explicitly: selecting, loading, extracting, generating, completed, cancelled, or failed. Keep UI-facing state on MainActor and move expensive document processing off the main actor. Creating a Task alone does not guarantee background execution.

Show progress, preserve the user’s question during recoverable errors, and make cancellation available. Swift cancellation is cooperative: check it between processing stages and stop further work where supported.

Use stable document identifiers in the app’s workspace. A filename is useful for display, but it should not be the sole key for authorization or retrieval.

Extract useful context before generation

For text-bearing PDFs, PDFKit’s PDFDocument exposes extracted text. Work page by page when answers need page references. A scanned PDF may require an OCR pipeline, and an encrypted or malformed document needs an understandable error.

Divide extracted text into manageable passages and retain their document and page identifiers. Retrieve the material relevant to the question instead of placing every document into every prompt.

This makes the answer easier to verify and helps manage context limits. A comparison should show where its material claims came from.

The app checks each operation against the user’s approved workspace.

Connect the model through a controlled Swift layer

For supported devices and configurations, the Foundation Models framework offers on-device generation through LanguageModelSession. Check SystemLanguageModel.default.availability before offering that path. Provide a clear unavailable state; never silently switch sensitive document processing to a cloud service.

A summarizer follows a fixed pipeline. An agent can choose additional steps, such as requesting another passage. Expose narrowly defined tools for approved document search and retrieval, with application-side validation on every call.

Treat retrieved text as untrusted input. Instructions embedded in a proposal cannot authorize another file, a network upload, or a write operation. Removing a document must block subsequent retrieval, including during an ongoing task.

Finish the mobile experience responsibly

Use SwiftUI’s fileExporter for user-directed exports and ShareLink where an explicit sharing workflow fits. Keep original documents unchanged unless editing is a deliberate feature with appropriate confirmation.

Before release, test on physical devices:

  • Availability: unsupported model configurations and offline file providers.
  • Responsiveness: large PDFs, memory pressure, and cancellation.
  • Boundaries: unselected files and instructions embedded in documents.
  • Retention: extracted text, temporary copies, indexes, and conversation history.

Stopping file access does not delete information already copied into app storage. Explain what a workspace removal action clears and any limits on deletion.

Conclusion

A useful SwiftUI document agent combines native file selection, deliberate Swift concurrency, grounded retrieval, and controlled model access. Start with a focused iPhone or iPad task, make failures understandable, and preserve the user’s authority from import to export. The quality of the mobile experience depends as much on those decisions as on the generated answer.

Author Details

Gautam Arora

Gautam has more than 12+ years of experience. He specializes in designing and architecting highly modular apps for mobile. He has worked with clients and teams across the US and UK.

Leave a Comment

Your email address will not be published. Required fields are marked *