Skip to content

Commit

Permalink
[MM-14899] Support sharing of screenshots via the iOS share extension (
Browse files Browse the repository at this point in the history
…#2697)

* Handle NSItemProvider item of type UIImage

* Create screenshot temp file with UploadSessionManager
  • Loading branch information
migbot authored and saturninoabril committed Apr 7, 2019
1 parent 64337b4 commit 6fef6d6
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions ios/MattermostShare/ShareViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6fef6d6

Please sign in to comment.