Commit 71cfd8421d

71cfd8421de8b3d3c1fbdc9f394e56b267ec23e1

parent: ec0e3dfe3b

Verified · cmc

cmc <hello@cleberg.net> · 2026-11-22 01:02 UTC

Scan documents into an attachment on iOS

More ▸ Attachments ▸ Scan Document… opens the document camera and saves
the pages as one PDF, linked at the caret like a taken photo.

Layout: unified · split

Sources/OrgstarMobile/DocumentScanner.swift added +52
@@ -0,0 +1,52 @@
1#if os(iOS)
2import SwiftUI
3import UIKit
4import VisionKit
5
6/// The document camera, for pages to attach; `scanned` gets them as one PDF, or nil when
7/// cancelled or failed.
8struct DocumentScanner: UIViewControllerRepresentable {
9 let scanned: (Data?) -> Void
10
11 static var isAvailable: Bool { VNDocumentCameraViewController.isSupported }
12
13 func makeUIViewController(context: Context) -> VNDocumentCameraViewController {
14 let scanner = VNDocumentCameraViewController()
15 scanner.delegate = context.coordinator
16 return scanner
17 }
18
19 func updateUIViewController(_ scanner: VNDocumentCameraViewController, context: Context) {}
20
21 func makeCoordinator() -> Coordinator { Coordinator(scanned: scanned) }
22
23 /// `pages` as a PDF, each page the size of its image in points.
24 static func pdf(_ pages: [UIImage]) -> Data {
25 UIGraphicsPDFRenderer(bounds: .zero).pdfData { context in
26 for page in pages {
27 let bounds = CGRect(origin: .zero, size: page.size)
28 context.beginPage(withBounds: bounds, pageInfo: [:])
29 page.draw(in: bounds)
30 }
31 }
32 }
33
34 final class Coordinator: NSObject, VNDocumentCameraViewControllerDelegate {
35 let scanned: (Data?) -> Void
36 init(scanned: @escaping (Data?) -> Void) { self.scanned = scanned }
37
38 func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFinishWith scan: VNDocumentCameraScan) {
39 let pages = (0..<scan.pageCount).map(scan.imageOfPage(at:))
40 scanned(pages.isEmpty ? nil : DocumentScanner.pdf(pages))
41 }
42
43 func documentCameraViewControllerDidCancel(_ controller: VNDocumentCameraViewController) {
44 scanned(nil)
45 }
46
47 func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFailWithError error: Error) {
48 scanned(nil)
49 }
50 }
51}
52#endif
Sources/OrgstarMobile/EditorScreen.swift +11
@@ -66,6 +66,7 @@ struct EditorScreen: View {
66 @State private var attaching: Attachments.Method? 66 @State private var attaching: Attachments.Method?
67 @State private var attachingPhotos = false 67 @State private var attachingPhotos = false
68 @State private var takingPhoto = false 68 @State private var takingPhoto = false
69 @State private var scanning = false
69 @State private var photos: [PhotosPickerItem] = [] 70 @State private var photos: [PhotosPickerItem] = []
70 /// A file shown in Quick Look. 71 /// A file shown in Quick Look.
71 @State private var previewed: URL? 72 @State private var previewed: URL?
@@ -165,6 +166,9 @@ struct EditorScreen: View {
165 if CameraPicker.isAvailable { 166 if CameraPicker.isAvailable {
166 Button("Take Photo…", systemImage: "camera") { takingPhoto = true } 167 Button("Take Photo…", systemImage: "camera") { takingPhoto = true }
167 } 168 }
169 if DocumentScanner.isAvailable {
170 Button("Scan Document…", systemImage: "doc.viewfinder") { scanning = true }
171 }
168 Button("Attach File from URL…", systemImage: "link.badge.plus") { attachActions.run("app.attach.url") } 172 Button("Attach File from URL…", systemImage: "link.badge.plus") { attachActions.run("app.attach.url") }
169 Button("New Attachment…", systemImage: "square.and.pencil") { attachActions.run("app.attach.new") } 173 Button("New Attachment…", systemImage: "square.and.pencil") { attachActions.run("app.attach.new") }
170 Button("Open Attachment…", systemImage: "paperclip") { attachActions.run("app.attach.open") } 174 Button("Open Attachment…", systemImage: "paperclip") { attachActions.run("app.attach.open") }
@@ -310,6 +314,13 @@ struct EditorScreen: View {
310 } 314 }
311 .ignoresSafeArea() 315 .ignoresSafeArea()
312 } 316 }
317 .fullScreenCover(isPresented: $scanning) {
318 DocumentScanner { data in
319 scanning = false
320 if let data { attachActions.yankImage(data, ext: "pdf", prefix: "scan") }
321 }
322 .ignoresSafeArea()
323 }
313 .onChange(of: photos) { _, items in 324 .onChange(of: photos) { _, items in
314 guard !items.isEmpty else { return } 325 guard !items.isEmpty else { return }
315 photos = [] 326 photos = []
docs/manual/guide/14-ios.org +1 −1
@@ -159,7 +159,7 @@ Incremental Search is in Commands, and on =C-s= and =C-r= with a hardware keyboa
159 159
160The org-timer commands are in Commands; a running timer shows above the text. See [[file:06-dates-and-clocking.org][Dates and clocking]]. 160The org-timer commands are in Commands; a running timer shows above the text. See [[file:06-dates-and-clocking.org][Dates and clocking]].
161 161
162More ▸ Attachments has the org-attach commands, Attach Photo or Video… and Take Photo…, which insert the photo's link at the caret; files shared to orgstar can be attached to an entry. Pasting an image and dropping files on the editor attach them and link them at the caret. Attachments are copied or moved: iOS doesn't let orgstar link a file from another app's storage, so the keys for attaching by hard link or symbolic link say "Attachments can't be linked on iPhone and iPad; Attach File… copies the file." See [[file:04-outlines.org][Outlines]] and [[file:09-links.org][Links]]. 162More ▸ Attachments has the org-attach commands, Attach Photo or Video…, Take Photo… and Scan Document…, which insert the link to the photo, or to the scanned pages as one PDF, at the caret; files shared to orgstar can be attached to an entry. Pasting an image and dropping files on the editor attach them and link them at the caret. Attachments are copied or moved: iOS doesn't let orgstar link a file from another app's storage, so the keys for attaching by hard link or symbolic link say "Attachments can't be linked on iPhone and iPad; Attach File… copies the file." See [[file:04-outlines.org][Outlines]] and [[file:09-links.org][Links]].
163 163
164Archive Subtree in Commands (or =C-c C-x C-s= with a hardware keyboard) archives the entry at the caret where =org-archive-location= says, as on the Mac. 164Archive Subtree in Commands (or =C-c C-x C-s= with a hardware keyboard) archives the entry at the caret where =org-archive-location= says, as on the Mac.
165 165
ios/Orgstar/Info.plist +1 −1
@@ -53,7 +53,7 @@
53 <key>NSCalendarsFullAccessUsageDescription</key> 53 <key>NSCalendarsFullAccessUsageDescription</key>
54 <string>The agenda shows your calendar events alongside your Org entries. orgstar changes your calendars only when you edit an event or add an entry to one.</string> 54 <string>The agenda shows your calendar events alongside your Org entries. orgstar changes your calendars only when you edit an event or add an entry to one.</string>
55 <key>NSCameraUsageDescription</key> 55 <key>NSCameraUsageDescription</key>
56 <string>Photos you take from the editor are saved with your notes and linked where the caret is.</string> 56 <string>Photos you take and documents you scan from the editor are saved with your notes and linked where the caret is.</string>
57 <key>NSFaceIDUsageDescription</key> 57 <key>NSFaceIDUsageDescription</key>
58 <string>Face ID unlocks the passphrase of encrypted entries you asked orgstar to remember.</string> 58 <string>Face ID unlocks the passphrase of encrypted entries you asked orgstar to remember.</string>
59 <key>NSRemindersFullAccessUsageDescription</key> 59 <key>NSRemindersFullAccessUsageDescription</key>
l10n/Localizable.xcstrings +34
@@ -26434,6 +26434,40 @@
26434 } 26434 }
26435 } 26435 }
26436 }, 26436 },
26437 "Scan Document…": {
26438 "localizations": {
26439 "de": {
26440 "stringUnit": {
26441 "state": "translated",
26442 "value": "Dokument scannen …"
26443 }
26444 },
26445 "es": {
26446 "stringUnit": {
26447 "state": "translated",
26448 "value": "Escanear documento…"
26449 }
26450 },
26451 "fr": {
26452 "stringUnit": {
26453 "state": "translated",
26454 "value": "Scanner un document…"
26455 }
26456 },
26457 "ja": {
26458 "stringUnit": {
26459 "state": "translated",
26460 "value": "書類をスキャン…"
26461 }
26462 },
26463 "pt-BR": {
26464 "stringUnit": {
26465 "state": "translated",
26466 "value": "Escanear Documento…"
26467 }
26468 }
26469 }
26470 },
26437 "Schedule": { 26471 "Schedule": {
26438 "comment": "Command title", 26472 "comment": "Command title",
26439 "localizations": { 26473 "localizations": {
scripts/build-ios-app.sh +1 −1
@@ -54,7 +54,7 @@ cat > "$app/Info.plist" <<PLIST
54 <key>MinimumOSVersion</key> 54 <key>MinimumOSVersion</key>
55 <string>26.0</string> 55 <string>26.0</string>
56 <key>NSCameraUsageDescription</key> 56 <key>NSCameraUsageDescription</key>
57 <string>Photos you take from the editor are saved with your notes and linked where the caret is.</string> 57 <string>Photos you take and documents you scan from the editor are saved with your notes and linked where the caret is.</string>
58 <key>NSFaceIDUsageDescription</key> 58 <key>NSFaceIDUsageDescription</key>
59 <string>Face ID unlocks the passphrase of encrypted entries you asked orgstar to remember.</string> 59 <string>Face ID unlocks the passphrase of encrypted entries you asked orgstar to remember.</string>
60 <key>NSCalendarsFullAccessUsageDescription</key> 60 <key>NSCalendarsFullAccessUsageDescription</key>