Skip to content

Control a real USB-connected iPhone from Linux (no Mac, no paid developer account) and expose it to AI agents as MCP tools

fix(device): pasteboard writes are local only (#122)

Qinghe Li307732856fbb
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b8a5828..e2a3519 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -27,6 +27,7 @@ All notable changes to this project are documented here. The format is based on
- A tap, swipe, key or launch that WebDriverAgent didn't answer in time no longer carries on later. WDA handles requests one at a time and keeps going after we stop waiting, so an action queued behind a slow one could land on the screen long after it was reported as failed. The daemon now ends that WDA session at once (`DELETE /session`, which WDA 16.15.3 answers outside its queue and which drops what is still queued in it) and opens a new one for the next action. A read that times out keeps its session.
- Reading the element tree of a screen with very many elements (long feeds and lists) no longer keeps taking as long. A read over 8 s, or one that times out, makes the next read ask WDA for a smaller snapshot (at most 150 children per element first, then half the depth and children, down to 12 levels and 60 children); a read under 1.5 s loosens it a step again. While the tree is cut short, `iphone_list_elements` and a `iphone_tap_element` that finds nothing say so.
- A tap by text, an element list or `then=elements` with a notification banner at the top read SpringBoard's tree instead of the app's (seen on a phone with the "Device Hub" banner: a button in Notes wasn't found). WDA tells the app in front by what is under one point, which by default is 20% of the short side from the top left corner: (86, 86) on a 430-point iPhone, under the banner. It is now moved to half the short side from that corner, (215, 215), once per WDA start.
+- The HID fallback's paste (non-ASCII text while WebDriverAgent is unavailable) writes to the phone's pasteboard as local only. It wrote without source metadata, and then the phone's pasteboard daemon offers the write to the owner's other Apple devices through Universal Clipboard, so the text typed, and the old contents put back afterwards, could replace a Mac's clipboard too (as Pinky's notes on the service describe; not checked on a Mac here). It now names itself in the write's source metadata, as Xcode does. The phone still asks to "Allow Paste" in Notes.
## [0.2.6] - 2026-11-14
diff --git a/CHANGELOG.zh-CN.md b/CHANGELOG.zh-CN.md
index 43f73d4..ba0aae6 100644
--- a/CHANGELOG.zh-CN.md
+++ b/CHANGELOG.zh-CN.md
@@ -27,6 +27,7 @@
- WebDriverAgent 没有及时应答的点击、滑动、按键、启动 App,不会再过一阵才执行。WDA 一个一个处理请求,我们不等了它还会接着做,排在慢请求后面的动作可能在报了失败之后很久才落到屏幕上。现在守护进程马上结束那个 WDA 会话(`DELETE /session`:WDA 16.15.3 不排队处理它,还会丢掉这个会话里还在排队的请求),下一个动作开新会话。读超时不结束会话。
- 元素特别多的界面(很长的信息流、列表)读元素树不再一直那么慢。一次读超过 8 秒或超时,下一次就让 WDA 少读一些(先限每个元素最多 150 个子元素,再把层数和子元素数减半,最少 12 层、60 个);读得快于 1.5 秒就放宽一级。元素树被截短时,`iphone_list_elements` 和没找到元素的 `iphone_tap_element` 会说明。
- 屏幕顶上弹着通知横幅时,按名字点击、读元素、`then=elements` 读到的是 SpringBoard 的元素树而不是 App 的(真机上弹着「Device Hub」横幅时,备忘录里的按钮找不到)。WDA 看屏幕上一个点下面是谁来判断前台 App,这个点默认在离左上角短边 20% 处,430 点宽的 iPhone 上是 (86, 86),正在横幅底下。现在每次 WDA 启动后挪到离左上角短边一半处,即 (215, 215)。
+- HID 后备路线的粘贴(WebDriverAgent 不可用时输入中文等)写手机剪贴板时只写本机。原来写入不带来源信息(`sourceMetadata`),手机的剪贴板服务会把它经「通用剪贴板」提供给机主的其他苹果设备,粘贴的文字和之后写回的原内容都可能覆盖 Mac 上的剪贴板(依据 Pinky 对这个服务的记录,这里没在 Mac 上核对)。现在写入时和 Xcode 一样带上来源信息。备忘录里照样会问「允许粘贴」。
## [0.2.6] - 2026-11-14
diff --git a/crates/ictl-device/src/pasteboard.rs b/crates/ictl-device/src/pasteboard.rs
index ae90912..3c93ad5 100644
--- a/crates/ictl-device/src/pasteboard.rs
+++ b/crates/ictl-device/src/pasteboard.rs
@@ -5,9 +5,11 @@
//!
//! 读用 idevice 的 [`PasteboardServiceClient`];写原来的内容要原样写回每一项、每一种类型,idevice 只提供写一段文字或一张
//! 图片,所以写由本模块自己发 `SET`(字段和 idevice `pasteboard_service.rs` 相同)。每次读写各开一条连接:上一条连接上
-//! 晚到的 `PULL_REPLY` 不会被当成 `SET_REPLY`。剪贴板的内容不写日志。
+//! 晚到的 `PULL_REPLY` 不会被当成 `SET_REPLY`。写入都带 `sourceMetadata`,只写本机(见 [`set_message`])。剪贴板的内容不写日志。
use std::borrow::Cow;
+use std::sync::OnceLock;
+use std::sync::atomic::{AtomicI64, Ordering};
use idevice::{
IdeviceError, ReadWrite, RemoteXpcClient, RsdService,
@@ -80,7 +82,21 @@ pub(crate) fn text_items(text: &str) -> Vec<Item> {
}]
}
-/// `SET` 消息:用 `items` 替换整个通用剪贴板
+/// 写入方的标识([`set_message`] 的 `sourceMetadata`):每个进程一个
+fn writer_nonce() -> &'static str {
+ static NONCE: OnceLock<String> = OnceLock::new();
+ NONCE.get_or_init(|| format!("iphone-control-{}", uuid::Uuid::new_v4()))
+}
+
+/// 本进程第几次写剪贴板([`set_message`] 的 `sourceMetadata`)
+static WRITES: AtomicI64 = AtomicI64::new(0);
+
+/// `SET` 消息:用 `items` 替换整个通用剪贴板。
+///
+/// 一定要带 `sourceMetadata`:手机上的 dtpasteboardd 只在有它时才按「只写本机」写入(UIPasteboard
+/// `setItemProviders:localOnly:expirationDate:`,Xcode 从电脑同步剪贴板也这样)。不带的话,写进去的内容会经「通用剪贴板」
+/// 提供给机主的其他苹果设备,把那边的剪贴板也换掉。字段同 Pinky(DIYgod/pinky `internal/driver/ios/pasteboard.go`,
+/// Apache-2.0):写入方的 nonce、名字、递增的计数
fn set_message(items: &[Item]) -> XPCObject {
let items = items
.iter()
@@ -101,7 +117,11 @@ fn set_message(items: &[Item]) -> XPCObject {
msg.insert("command".into(), XPCObject::String("SET".into()));
msg.insert("pasteboardName".into(), XPCObject::String(GENERAL_PASTEBOARD.into()));
msg.insert("items".into(), XPCObject::Array(items));
- msg.insert("sourceMetadata".into(), XPCObject::Null);
+ let mut source = Dictionary::new();
+ source.insert("nonce".into(), XPCObject::String(writer_nonce().into()));
+ source.insert("pasteboardName".into(), XPCObject::String("iphone-control".into()));
+ source.insert("changeCount".into(), XPCObject::Int64(WRITES.fetch_add(1, Ordering::Relaxed) + 1));
+ msg.insert("sourceMetadata".into(), XPCObject::Dictionary(source));
XPCObject::Dictionary(msg)
}
@@ -214,7 +234,11 @@ mod tests {
let XPCObject::Dictionary(msg) = set_message(&text_items("你好")) else { panic!("not a dictionary") };
assert!(matches!(&msg["command"], XPCObject::String(s) if s == "SET"));
assert!(matches!(&msg["pasteboardName"], XPCObject::String(s) if s == "general"));
- assert!(matches!(msg["sourceMetadata"], XPCObject::Null));
+ // 带 sourceMetadata 才只写本机,不经「通用剪贴板」同步到别的设备
+ let XPCObject::Dictionary(source) = &msg["sourceMetadata"] else { panic!("sourceMetadata") };
+ assert!(matches!(&source["nonce"], XPCObject::String(n) if n.starts_with("iphone-control-")));
+ assert!(matches!(&source["pasteboardName"], XPCObject::String(n) if n == "iphone-control"));
+ assert!(matches!(source["changeCount"], XPCObject::Int64(n) if n >= 1));
let XPCObject::Array(items) = &msg["items"] else { panic!("items") };
let XPCObject::Dictionary(item) = &items[0] else { panic!("item") };
let XPCObject::Array(types) = &item["types"] else { panic!("types") };