Skip to content

Linux + iPhone Continuity / iMessage / SMS

feat(ios): share extension sends image to clipboard

Zack Bartel2d5c28deaf2a
diff --git a/apple/TetherFramework/Sources/TetherFramework/SharePayload.swift b/apple/TetherFramework/Sources/TetherFramework/SharePayload.swift
index d1d0b9f..d8ac430 100644
--- a/apple/TetherFramework/Sources/TetherFramework/SharePayload.swift
+++ b/apple/TetherFramework/Sources/TetherFramework/SharePayload.swift
@@ -23,4 +23,7 @@ public enum SharePayload: Sendable {
/// Send binary data as a file transfer (`file_start` / `file_chunk` / `file_end`).
case file(Data, filename: String)
+
+ /// Set the Linux clipboard to a PNG image.
+ case clipboardImage(Data)
}
diff --git a/apple/TetherFramework/Sources/TetherFramework/ShareSender.swift b/apple/TetherFramework/Sources/TetherFramework/ShareSender.swift
index db73f77..a5bc40c 100644
--- a/apple/TetherFramework/Sources/TetherFramework/ShareSender.swift
+++ b/apple/TetherFramework/Sources/TetherFramework/ShareSender.swift
@@ -23,6 +23,7 @@ public enum ShareSenderError: LocalizedError {
case connectionFailed(String)
case timeout
case fileTransferFailed(String)
+ case clipboardImageUnsupported
public var errorDescription: String? {
switch self {
@@ -38,6 +39,8 @@ public enum ShareSenderError: LocalizedError {
return "Connection timed out. Make sure tetherd is running on your desktop."
case .fileTransferFailed(let msg):
return "File transfer failed: \(msg)"
+ case .clipboardImageUnsupported:
+ return "Update Tether on your computer to copy images."
}
}
}
@@ -95,6 +98,16 @@ public actor ShareSender {
case .file(let data, let filename):
sendResult = await sendFile(data: data, filename: filename, via: connection)
+
+ case .clipboardImage(let png):
+ // An old daemon would save the image to Downloads instead.
+ if !(await daemonSupportsClipboardImages(via: connection)) {
+ sendResult = .failure(ShareSenderError.clipboardImageUnsupported)
+ } else if png.count > 32 * 1024 * 1024 { // same cap the daemon enforces
+ sendResult = .failure(ShareSenderError.fileTransferFailed("Image is too large (32 MB max)."))
+ } else {
+ sendResult = await sendFile(data: png, filename: "clipboard.png", clipboard: "set", via: connection)
+ }
}
connection.disconnect()
@@ -214,9 +227,33 @@ public actor ShareSender {
// MARK: - Private — File Transfer
+ /// Whether the daemon answers `hello` with `clipboard_image`. Old daemons
+ /// reply "OK", which never decodes as `hello`, so they time out to false.
+ private static func daemonSupportsClipboardImages(via connection: TetherConnection) async -> Bool {
+ await withCheckedContinuation { continuation in
+ var resumed = false
+
+ connection.onMessage = { message in
+ guard !resumed, message.parsedCommand == .hello else { return }
+ resumed = true
+ continuation.resume(returning: message.features?.contains("clipboard_image") ?? false)
+ }
+ connection.send(.hello(features: ["clipboard_image"]))
+
+ Task {
+ try? await Task.sleep(for: .seconds(3))
+ if !resumed {
+ resumed = true
+ continuation.resume(returning: false)
+ }
+ }
+ }
+ }
+
private static func sendFile(
data fileData: Data,
filename: String,
+ clipboard: String? = nil,
via connection: TetherConnection
) async -> Result<Void, Error> {
let transferId = UUID().uuidString
@@ -244,7 +281,7 @@ public actor ShareSender {
}
// Send file_start
- connection.send(.fileStart(filename: filename, size: totalSize, transferId: transferId))
+ connection.send(.fileStart(filename: filename, size: totalSize, transferId: transferId, clipboard: clipboard))
// Stream chunks
Task {
diff --git a/apple/TetherShareExtension/ShareViewController.swift b/apple/TetherShareExtension/ShareViewController.swift
index af5d2f6..19c9cbc 100644
--- a/apple/TetherShareExtension/ShareViewController.swift
+++ b/apple/TetherShareExtension/ShareViewController.swift
@@ -8,7 +8,8 @@
// a SwiftUI sheet with only the routes applicable to that content:
//
// • Text (single item) → 📋 Send to Clipboard | 🔑 Send as OTP
-// • Image/Video/Contact/Audio/File (single item) → 📁 Send as File
+// • Image (single item) → 📋 Copy to Clipboard | 📁 Send as File
+// • Video/Contact/Audio/File (single item) → 📁 Send as File
// • Multiple items (Photos multi-select, etc.) → 📁 Send N Files
//
// Every attachment on every input item is resolved (not just the first),
@@ -360,6 +361,22 @@ private struct ShareSheetView: View {
}
case .file(let data, let filename):
+ if UTType(filenameExtension: (filename as NSString).pathExtension)?.conforms(to: .image) == true {
+ ActionButton(
+ icon: "clipboard",
+ label: "Copy to Clipboard",
+ subtitle: "Paste the image on your desktop",
+ color: .cyan
+ ) {
+ // The daemon only takes PNG; this also converts JPEG/HEIC.
+ if let png = UIImage(data: data)?.pngData() {
+ perform(.clipboardImage(png))
+ } else {
+ resultMessage = "Couldn't read this image."
+ didFail = true
+ }
+ }
+ }
ActionButton(
icon: "arrow.up.doc.fill",
label: "Send as File",
@@ -535,6 +552,7 @@ private struct ShareSheetView: View {
case .openUrl: return "Opened on desktop ✓"
case .otp: return "OTP stored in vault ✓"
case .file(_, let name): return "\(name) sent ✓"
+ case .clipboardImage: return "Copied to clipboard ✓"
}
}