From 6fef6d6b921e9ffb8f782eb0076df04749612a73 Mon Sep 17 00:00:00 2001 From: Miguel Alatzar Date: Sun, 7 Apr 2019 14:30:25 -0700 Subject: [PATCH] [MM-14899] Support sharing of screenshots via the iOS share extension (#2697) * Handle NSItemProvider item of type UIImage * Create screenshot temp file with UploadSessionManager --- ios/MattermostShare/ShareViewController.swift | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/ios/MattermostShare/ShareViewController.swift b/ios/MattermostShare/ShareViewController.swift index cdfc10835..a4136f333 100644 --- a/ios/MattermostShare/ShareViewController.swift +++ b/ios/MattermostShare/ShareViewController.swift @@ -19,6 +19,7 @@ class ShareViewController: SLComposeServiceViewController { private var serverURL: String? private var message: String? private var publicURL: String? + private var tempContainerURL: URL? = UploadSessionManager.shared.tempContainerURL() as URL? fileprivate var selectedChannel: Item? fileprivate var selectedTeam: Item? @@ -195,6 +196,19 @@ class ShareViewController: SLComposeServiceViewController { attachment?.type = kUTTypeImage as String self.attachments.append(attachment!) } + } else if let image = item as? UIImage { + if let data = image.pngData() { + let tempImageURL = self.tempContainerURL? + .appendingPathComponent(UUID().uuidString) + .appendingPathExtension(".png") + if (try? data.write(to: tempImageURL!)) != nil { + let attachment = self.saveAttachment(url: tempImageURL!) + if (attachment != nil) { + attachment?.type = kUTTypeImage as String + self.attachments.append(attachment!) + } + } + } } } self.dispatchGroup.leave() @@ -339,16 +353,17 @@ class ShareViewController: SLComposeServiceViewController { } func saveAttachment(url: URL) -> AttachmentItem? { - let tempURL: URL? = UploadSessionManager.shared.tempContainerURL() as URL? let fileMgr = FileManager.default let fileName = url.lastPathComponent - let tempFileURL = tempURL?.appendingPathComponent(fileName) + let tempFileURL = tempContainerURL?.appendingPathComponent(fileName) do { - try? FileManager.default.removeItem(at: tempFileURL!) - try fileMgr.copyItem(at: url, to: tempFileURL!) - let attr = try fileMgr.attributesOfItem(atPath: (tempFileURL?.path)!) as NSDictionary + if (tempFileURL != url) { + try? FileManager.default.removeItem(at: tempFileURL!) + try fileMgr.copyItem(at: url, to: tempFileURL!) + } + let attr = try fileMgr.attributesOfItem(atPath: (tempFileURL?.path)!) as NSDictionary let attachment = AttachmentItem() attachment.fileName = fileName attachment.fileURL = tempFileURL