diff --git a/ShareExtension/Info.plist b/ShareExtension/Info.plist index 1602c816..8df5f5dd 100644 --- a/ShareExtension/Info.plist +++ b/ShareExtension/Info.plist @@ -8,6 +8,8 @@ NSExtensionActivationRule + NSExtensionActivationSupportsText + NSExtensionActivationSupportsImageWithMaxCount 4 NSExtensionActivationSupportsMovieWithMaxCount diff --git a/ShareExtension/ShareViewController.swift b/ShareExtension/ShareViewController.swift index e3000b3f..a74ab0ae 100644 --- a/ShareExtension/ShareViewController.swift +++ b/ShareExtension/ShareViewController.swift @@ -71,36 +71,43 @@ class ShareViewController: UIViewController { private func getDraftConfigurationFromExtensionContext() async -> (String, [DraftAttachment]) { guard let extensionContext, - let inputItem = (extensionContext.inputItems as? [NSExtensionItem])?.first, - let itemProvider = inputItem.attachments?.first else { + let inputItem = (extensionContext.inputItems as? [NSExtensionItem])?.first else { return ("", []) } - if let url: NSURL = await getObject(from: itemProvider) { - if let title = inputItem.attributedTitle ?? inputItem.attributedContentText { - return ("\n\n\(title.string)\n\(url.absoluteString ?? "")", []) - } else { - return ("\n\n\(url.absoluteString ?? "")", []) - } - } else if let text: NSString = await getObject(from: itemProvider) { - return ("\n\n\(text)", []) - } else if let attributedContent = inputItem.attributedContentText { - return ("\n\n\(attributedContent.string)", []) - } else { - let attachments = await withTaskGroup(of: DraftAttachment?.self, returning: [DraftAttachment].self) { group in - for provider in inputItem.attachments! { - group.addTask { @MainActor in - await self.getObject(from: provider) - } + var text: String = "" + var url: URL? + var attachments: [DraftAttachment] = [] + + for itemProvider in inputItem.attachments ?? [] { + if let attached: NSURL = await getObject(from: itemProvider) { + if url == nil { + url = attached as URL } - - return await group.reduce(into: [], { partialResult, result in - if let result { - partialResult.append(result) - } - }) + } else if let s: NSString = await getObject(from: itemProvider) { + if text.isEmpty { + text = s as String + } + } else if let attachment: DraftAttachment = await getObject(from: itemProvider) { + attachments.append(attachment) } - return ("", attachments) } + + if text.isEmpty, + let s = inputItem.attributedTitle ?? inputItem.attributedContentText { + text = s.string + } + + if let url { + if !text.isEmpty { + text += "\n" + } + text += url.absoluteString + } + + if !text.isEmpty { + text = "\n\n\(text)" + } + return (text, attachments) } private func getObject(from itemProvider: NSItemProvider) async -> T? {