Skip to content

Commit

Permalink
addded paste method channel in native ios folder (#462)
Browse files Browse the repository at this point in the history
  • Loading branch information
krrish-sehgal authored Oct 30, 2024
1 parent df46f31 commit b678da1
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,42 @@
import UIKit
import Flutter
import UIKit

@main
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ 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))
}
}
}

0 comments on commit b678da1

Please sign in to comment.