From 6a2ee9278fe62e822eb3aa6a260c77942b97ffa7 Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Mon, 10 Feb 2020 14:23:10 +0100 Subject: [PATCH] fix(ios): Make Clipboard plugin return errors (#2430) --- .../Capacitor/Plugins/Clipboard.swift | 52 ++++++++++++------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/ios/Capacitor/Capacitor/Plugins/Clipboard.swift b/ios/Capacitor/Capacitor/Plugins/Clipboard.swift index 2f1c5e70d..6bfd992cb 100644 --- a/ios/Capacitor/Capacitor/Plugins/Clipboard.swift +++ b/ios/Capacitor/Capacitor/Plugins/Clipboard.swift @@ -23,32 +23,46 @@ public class CAPClipboardPlugin : CAPPlugin { @objc func read(_ call: CAPPluginCall) { let type = call.options["type"] as? String ?? "string" - - if type == "string" && UIPasteboard.general.hasStrings { - call.success([ - "value": UIPasteboard.general.string! - ]) - return - } - - if type == "url" && UIPasteboard.general.hasURLs { - let url = UIPasteboard.general.url! - call.success([ - "value": url.absoluteString - ]) + + if type == "string" { + if UIPasteboard.general.hasStrings { + call.success([ + "value": UIPasteboard.general.string! + ]) + } else { + call.error("Unable to get string from clipboard") + } return } - - if type == "image" && UIPasteboard.general.hasImages { - let image = UIPasteboard.general.image! - let data = image.pngData() - if let base64 = data?.base64EncodedString() { + + if type == "url" { + if UIPasteboard.general.hasURLs { + let url = UIPasteboard.general.url! call.success([ - "value": base64 + "value": url.absoluteString ]) + } else { + call.error("Unable to get url from clipboard") } return } + + if type == "image" { + if UIPasteboard.general.hasImages { + let image = UIPasteboard.general.image! + let data = image.pngData() + if let base64 = data?.base64EncodedString() { + call.success([ + "value": base64 + ]) + } + } else { + call.error("Unable to get image from clipboard") + } + return + } + + call.error("Invalid type") } }