Skip to content

Commit

Permalink
fix(ios): Make Clipboard plugin return errors (#2430)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Feb 10, 2020
1 parent 6fe6d25 commit 6a2ee92
Showing 1 changed file with 33 additions and 19 deletions.
52 changes: 33 additions & 19 deletions ios/Capacitor/Capacitor/Plugins/Clipboard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down

0 comments on commit 6a2ee92

Please sign in to comment.