Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

tray/ios: accept documents from the share sheet, Open in, and AirDrop

nyblnet76c40a90997e
diff --git a/tray/README.md b/tray/README.md
index c0135c8a..0e93e825 100644
--- a/tray/README.md
+++ b/tray/README.md
@@ -86,6 +86,29 @@ of this bridge did exactly that and prompted on every single save.
- **`bridge.js` is injected `.atDocumentStart`.** Bento decides whether it can
save during boot; injected later, the editor has already concluded it cannot.
+## Getting documents in
+
+Four routes, all landing on the same in-place editing:
+
+1. **Files** — the app's folder appears under *On My iPhone → Bento Tray*, and
+ the Browse tab navigates the whole Files hierarchy: iCloud Drive, Dropbox,
+ Google Drive, anything with a File Provider. Tap a document to open it where
+ it lives; edits go back to that file.
+2. **Share sheet / "Open in"** — from Safari, Mail, Messages. The app declares
+ itself an `Editor` for `public.html` with `LSSupportsOpeningDocumentsInPlace`,
+ so it is offered for any HTML file.
+3. **AirDrop**, same mechanism.
+4. **"+"** for a new document from the bundled seed.
+
+Routes 2 and 3 need `scene(_:openURLContexts:)` — declaring the document type
+only makes the app *offered*, it does not deliver the file. Both cold launch
+(`options.urlContexts`) and warm delivery are handled, and the URL is wrapped in
+a security-scoped accessor: without it the read fails silently and the document
+opens blank.
+
+`LSHandlerRank` is `Alternate`, so Safari stays the default for HTML and Bento
+Tray appears as a choice rather than hijacking every `.html` on the device.
+
## Building
Needs **full Xcode** (Command Line Tools alone is not enough) and XcodeGen:
diff --git a/tray/ios/DocumentBrowserViewController.swift b/tray/ios/DocumentBrowserViewController.swift
index 04ed2fca..856d2b23 100644
--- a/tray/ios/DocumentBrowserViewController.swift
+++ b/tray/ios/DocumentBrowserViewController.swift
@@ -67,6 +67,21 @@ final class DocumentBrowserViewController: UIDocumentBrowserViewController,
openEditor(url)
}
+ /// Open a document handed to us by the SYSTEM — the share sheet, "Open in",
+ /// AirDrop, or tapping a file in Files. `revealDocument` imports it into the
+ /// browser's storage when it lives somewhere we cannot keep hold of, and
+ /// surfaces it in the UI so the user can find it again afterwards.
+ func openIncoming(_ url: URL) {
+ revealDocument(at: url, importIfNeeded: true) { [weak self] revealed, error in
+ guard let self else { return }
+ // Falling back to the original URL matters: reveal fails for a file
+ // already inside our own container, which is exactly where a
+ // previously-imported document lives.
+ self.openEditor(revealed ?? url)
+ if let error { NSLog("reveal failed, opened in place: %@", String(describing: error)) }
+ }
+ }
+
private func openEditor(_ url: URL) {
// Security-scoped access: a document opened in place lives outside the
// app container, so the URL must be scoped for the whole editing
diff --git a/tray/ios/SceneDelegate.swift b/tray/ios/SceneDelegate.swift
index 409d927d..c6c5d3f5 100644
--- a/tray/ios/SceneDelegate.swift
+++ b/tray/ios/SceneDelegate.swift
@@ -13,5 +13,25 @@ final class SceneDelegate: UIResponder, UIWindowSceneDelegate {
w.rootViewController = DocumentBrowserViewController()
w.makeKeyAndVisible()
window = w
+ // COLD LAUNCH: the system can hand us a file as it starts the scene.
+ // Deferred a runloop so the browser is on screen before it presents.
+ if let url = options.urlContexts.first?.url {
+ DispatchQueue.main.async { [weak self] in self?.open(url) }
+ }
+ }
+
+ /// WARM: the app is already running and the system hands it another file.
+ func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
+ guard let url = URLContexts.first?.url else { return }
+ open(url)
+ }
+
+ /// A file arriving from outside is security-scoped: without the accessor the
+ /// read fails silently and the document opens empty.
+ private func open(_ url: URL) {
+ guard let browser = window?.rootViewController as? DocumentBrowserViewController else { return }
+ let scoped = url.startAccessingSecurityScopedResource()
+ defer { if scoped { url.stopAccessingSecurityScopedResource() } }
+ browser.openIncoming(url)
}
}