diff --git a/packages/pasteboard/ios/pasteboard/Sources/pasteboard/SwiftPasteboardPlugin.swift b/packages/pasteboard/ios/pasteboard/Sources/pasteboard/SwiftPasteboardPlugin.swift index d953e36e..80fbafec 100644 --- a/packages/pasteboard/ios/pasteboard/Sources/pasteboard/SwiftPasteboardPlugin.swift +++ b/packages/pasteboard/ios/pasteboard/Sources/pasteboard/SwiftPasteboardPlugin.swift @@ -3,6 +3,10 @@ import UIKit @objc(PasteboardPlugin) public class PasteboardPlugin: NSObject, FlutterPlugin { + // Pasteboard access can block, e.g. while another app still provides the + // data, so image and writeImage run off the main thread, in call order. + private static let queue = DispatchQueue(label: "one.mixin.pasteboard", qos: .userInitiated) + public static func register(with registrar: FlutterPluginRegistrar) { let channel = FlutterMethodChannel(name: "pasteboard", binaryMessenger: registrar.messenger()) let instance = PasteboardPlugin() @@ -32,10 +36,11 @@ public class PasteboardPlugin: NSObject, FlutterPlugin { } } - private func image(result: FlutterResult) { - let image = UIPasteboard.general.image - let data = image?.pngData() - result(data) + private func image(result: @escaping FlutterResult) { + Self.queue.async { + let data = UIPasteboard.general.image?.pngData() + DispatchQueue.main.async { result(data) } + } } private func files(result: FlutterResult) { @@ -46,10 +51,11 @@ public class PasteboardPlugin: NSObject, FlutterPlugin { result(nil) } - private func writeImageToPasteboard(_ data: Data, result: FlutterResult) { - let image = UIImage(data: data) - UIPasteboard.general.image = image - result(nil) + private func writeImageToPasteboard(_ data: Data, result: @escaping FlutterResult) { + Self.queue.async { + UIPasteboard.general.image = UIImage(data: data) + DispatchQueue.main.async { result(nil) } + } } } diff --git a/packages/pasteboard/lib/pasteboard.dart b/packages/pasteboard/lib/pasteboard.dart index 158504eb..97c037d5 100644 --- a/packages/pasteboard/lib/pasteboard.dart +++ b/packages/pasteboard/lib/pasteboard.dart @@ -8,6 +8,10 @@ class Pasteboard { /// Returns the image data of the pasteboard. /// /// available on iOS, Desktop, Web, Android + /// + /// iOS: runs off the main thread, in order with [writeImage]. Await it + /// before calling [text], [writeText] or Flutter's `Clipboard` if the + /// order matters. static Future get image => pasteboard.image; /// only available on Windows and the web. @@ -18,6 +22,10 @@ class Pasteboard { /// available on iOS, Android, Web /// /// set image data to system pasteboard. + /// + /// iOS: runs off the main thread, in order with [image]. Await it before + /// calling [text], [writeText] or Flutter's `Clipboard` if the order + /// matters. static Future writeImage(Uint8List? image) => pasteboard.writeImage(image);