From 7c5dfc47509a5dd333beea513b5e891709a3c3dd Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Wed, 26 Aug 2020 20:03:25 +0200 Subject: [PATCH 1/2] feat: Add extension for creating data from data url --- ios/Capacitor/Capacitor/CAPData.swift | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 ios/Capacitor/Capacitor/CAPData.swift diff --git a/ios/Capacitor/Capacitor/CAPData.swift b/ios/Capacitor/Capacitor/CAPData.swift new file mode 100644 index 000000000..1105c581a --- /dev/null +++ b/ios/Capacitor/Capacitor/CAPData.swift @@ -0,0 +1,22 @@ +extension Data: CapacitorExtension {} +public extension CapacitorExtensionTypeWrapper where T == Data { + + static func data(base64EncodedOrDataUrl: String) -> Data? { + if isBase64DataUrl(base64EncodedOrDataUrl) { + if let url = URL(string: base64EncodedOrDataUrl) { + do { + return try T(contentsOf: url) + } catch { + return nil + } + } + return nil + } else { + return T(base64Encoded: base64EncodedOrDataUrl) + } + } + + private static func isBase64DataUrl(_ base64EncodedOrDataUrl: String) -> Bool { + return base64EncodedOrDataUrl.starts(with: "data:") && base64EncodedOrDataUrl.contains("base64,") + } +} From 054cab89f53abf03121c798ea069d07e4752faef Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Thu, 27 Aug 2020 13:29:09 +0200 Subject: [PATCH 2/2] rename extension --- ios/Capacitor/Capacitor/{CAPData.swift => Data+Capacitor.swift} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename ios/Capacitor/Capacitor/{CAPData.swift => Data+Capacitor.swift} (100%) diff --git a/ios/Capacitor/Capacitor/CAPData.swift b/ios/Capacitor/Capacitor/Data+Capacitor.swift similarity index 100% rename from ios/Capacitor/Capacitor/CAPData.swift rename to ios/Capacitor/Capacitor/Data+Capacitor.swift