diff --git a/ios/Classes/RSIShareViewController.swift b/ios/Classes/RSIShareViewController.swift index 0c83fbb3..15b3bbcd 100644 --- a/ios/Classes/RSIShareViewController.swift +++ b/ios/Classes/RSIShareViewController.swift @@ -9,19 +9,11 @@ import UIKit import Social import MobileCoreServices import Photos -import UniformTypeIdentifiers open class RSIShareViewController: SLComposeServiceViewController { var hostAppBundleIdentifier = "" var appGroupId = "" var sharedMedia: [SharedMediaFile] = [] - private let supportedContentTypes: [UTType] = [ - .image, - .movie, - .text, - .url, - .fileURL - ] open override func isContentValid() -> Bool { return true @@ -41,27 +33,27 @@ open class RSIShareViewController: SLComposeServiceViewController { if let content = extensionContext!.inputItems[0] as? NSExtensionItem { if let contents = content.attachments as? [NSItemProvider] { for (index, attachment) in (contents).enumerated() { - for contentType in supportedContentTypes { - if attachment.hasItemConformingToTypeIdentifier(contentType.identifier) { - attachment.loadItem(forTypeIdentifier: contentType.identifier) { [weak self] data, error in + for type in SharedMediaType.allCases { + if attachment.hasItemConformingToTypeIdentifier(type.toUTTypeIdentifier) { + attachment.loadItem(forTypeIdentifier: type.toUTTypeIdentifier) { [weak self] data, error in guard let this = self, error == nil else { self?.dismissWithError() return } - switch contentType { + switch type { case .text, .url: if let text = data as? String { this.handleMedia(forLiteral: text, - contentType: contentType, - index: index, - content: content) + type: type, + index: index, + content: content) } default: if let url = data as? URL { this.handleMedia(forFile: url, - contentType: contentType, - index: index, - content: content) + type: type, + index: index, + content: content) } } } @@ -98,23 +90,23 @@ open class RSIShareViewController: SLComposeServiceViewController { } - private func handleMedia(forLiteral item: String, contentType: UTType, index: Int, content: NSExtensionItem) { + private func handleMedia(forLiteral item: String, type: SharedMediaType, index: Int, content: NSExtensionItem) { sharedMedia.append(SharedMediaFile( path: item, - mimeType: contentType == .text ? "text/plain": nil, - type: contentType == .url ? .url : .text + mimeType: type == .text ? "text/plain": nil, + type: type )) if index == (content.attachments?.count ?? 0) - 1 { saveAndRedirect() } } - private func handleMedia(forFile url: URL, contentType: UTType, index: Int, content: NSExtensionItem) { - let fileName = getFileName(from: url, contentType: contentType) + private func handleMedia(forFile url: URL, type: SharedMediaType, index: Int, content: NSExtensionItem) { + let fileName = getFileName(from: url, type: type) let newPath = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroupId)!.appendingPathComponent(fileName) if copyFile(at: url, to: newPath) { - if contentType == .movie { + if type == .video { // Get video thumbnail and duration if let videoInfo = getVideoInfo(from: url) { sharedMedia.append(SharedMediaFile( @@ -122,14 +114,14 @@ open class RSIShareViewController: SLComposeServiceViewController { mimeType: url.mimeType(), thumbnail: videoInfo.thumbnail, duration: videoInfo.duration, - type: .video + type: type )) } } else { sharedMedia.append(SharedMediaFile( path: newPath.absoluteString, mimeType: url.mimeType(), - type: contentType == .image ? .image : .file + type: type )) } } @@ -177,18 +169,18 @@ open class RSIShareViewController: SLComposeServiceViewController { extensionContext!.completeRequest(returningItems: [], completionHandler: nil) } - private func getFileName(from url: URL, contentType: UTType) -> String { + private func getFileName(from url: URL, type: SharedMediaType) -> String { var name = url.lastPathComponent if name.isEmpty { - switch contentType { + switch type { case .image: name = UUID().uuidString + ".png" - case .movie: + case .video: name = UUID().uuidString + ".mp4" case .text: name = UUID().uuidString + ".txt" default: - name = "" + name = UUID().uuidString } } return name @@ -256,22 +248,3 @@ extension URL { } } } - -extension UTType { - public func toMediaType() -> SharedMediaType { - switch self { - case .image: - return .image - case .movie: - return .video - case .text: - return .text - case .fileURL: - return .file - case .url: - return .url - default: - return .file - } - } -} diff --git a/ios/Classes/SwiftReceiveSharingIntentPlugin.swift b/ios/Classes/SwiftReceiveSharingIntentPlugin.swift index 25ffed7d..5bab935e 100644 --- a/ios/Classes/SwiftReceiveSharingIntentPlugin.swift +++ b/ios/Classes/SwiftReceiveSharingIntentPlugin.swift @@ -218,10 +218,28 @@ public class SharedMediaFile: Codable { } } -public enum SharedMediaType: String, Codable { +public enum SharedMediaType: String, Codable, CaseIterable { case image case video case text +// case audio case file case url + + public var toUTTypeIdentifier: String { + switch self { + case .image: + return "public.image" + case .video: + return "public.movie" + case .text: + return "public.text" +// case .audio: +// return "public.audio" + case .file: + return "public.file-url" + case .url: + return "public.url" + } + } } diff --git a/ios/receive_sharing_intent.podspec b/ios/receive_sharing_intent.podspec index b8532d9c..f8e61077 100644 --- a/ios/receive_sharing_intent.podspec +++ b/ios/receive_sharing_intent.podspec @@ -3,7 +3,7 @@ # Pod::Spec.new do |s| s.name = 'receive_sharing_intent' - s.version = '1.5.3' + s.version = '1.6.1' s.summary = 'A flutter plugin that enables flutter apps to receive sharing photos from other apps.' s.description = <<-DESC A flutter plugin that enables flutter apps to receive sharing photos from other apps.