Skip to content

Commit

Permalink
Stop using UTType because its not supported in iOS <14
Browse files Browse the repository at this point in the history
  • Loading branch information
KasemJaffer committed Jan 26, 2024
1 parent 8c2a757 commit 9283367
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 51 deletions.
71 changes: 22 additions & 49 deletions ios/Classes/RSIShareViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}
}
Expand Down Expand Up @@ -98,38 +90,38 @@ 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(
path: newPath.absoluteString,
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
))
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
}
20 changes: 19 additions & 1 deletion ios/Classes/SwiftReceiveSharingIntentPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
2 changes: 1 addition & 1 deletion ios/receive_sharing_intent.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 9283367

Please sign in to comment.