From b678da1710c8432403f4a92035c4328e160730d0 Mon Sep 17 00:00:00 2001 From: Krrish Sehgal <133865424+krrish-sehgal@users.noreply.github.com> Date: Thu, 31 Oct 2024 03:52:34 +0530 Subject: [PATCH] addded paste method channel in native ios folder (#462) --- ios/Runner/AppDelegate.swift | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/ios/Runner/AppDelegate.swift b/ios/Runner/AppDelegate.swift index b636303481..be8434e74b 100644 --- a/ios/Runner/AppDelegate.swift +++ b/ios/Runner/AppDelegate.swift @@ -1,5 +1,5 @@ -import UIKit import Flutter +import UIKit @main @objc class AppDelegate: FlutterAppDelegate { @@ -7,7 +7,36 @@ import Flutter _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { + // Register the method channel + let controller = window?.rootViewController as! FlutterViewController + let clipboardImageChannel = FlutterMethodChannel(name: "clipboard_image_channel", + binaryMessenger: controller.binaryMessenger) + + clipboardImageChannel.setMethodCallHandler { (call: FlutterMethodCall, result: @escaping FlutterResult) in + if call.method == "getClipboardImage" { + self.getClipboardImage(result: result) + } else { + result(FlutterMethodNotImplemented) + } + } + GeneratedPluginRegistrant.register(with: self) return super.application(application, didFinishLaunchingWithOptions: launchOptions) } -} + + private func getClipboardImage(result: FlutterResult) { + // Check if the clipboard contains an image + if let image = UIPasteboard.general.image { + // Convert the image to PNG data + if let imageData = image.pngData() { + // Encode the image data to a Base64 string + let base64String = imageData.base64EncodedString() + result(base64String) // Send the Base64 string back to Flutter + } else { + result(FlutterError(code: "NO_IMAGE", message: "Could not convert image to data", details: nil)) + } + } else { + result(FlutterError(code: "NO_IMAGE", message: "Clipboard does not contain an image", details: nil)) + } + } +} \ No newline at end of file