Skip to content

Commit

Permalink
Use UIImagePickerController to pick videos
Browse files Browse the repository at this point in the history
  • Loading branch information
dragermrb committed Oct 30, 2021
1 parent fe5d1b8 commit c550c77
Showing 1 changed file with 93 additions and 33 deletions.
126 changes: 93 additions & 33 deletions ios/Plugin/FilePickerPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,45 +21,51 @@ public class FilePickerPlugin: CAPPlugin {
{
var extUTI:CFString?

switch mimeType {
case "audio/*":
extUTI = kUTTypeAudio
case "image/*":
extUTI = kUTTypeImage
case "text/*":
extUTI = kUTTypeText
case "video/*":
extUTI = kUTTypeVideo
default:
extUTI = kUTTypeData

if mimeType.range(of: "*") == nil {
let utiUnmanaged = UTTypeCreatePreferredIdentifierForTag(
kUTTagClassMIMEType,
mimeType as CFString,
nil
)

if let uti = (utiUnmanaged?.takeRetainedValue() as String?) {
if !uti.hasPrefix("dyn.") {
extUTI = uti as CFString
}
if (mimeType.starts(with: "audio/*")) {
extUTI = kUTTypeAudio
} else if (mimeType.starts(with: "image/*")) {
extUTI = kUTTypeImage
} else if (mimeType.starts(with: "text/*")) {
extUTI = kUTTypeText
} else if (mimeType.starts(with: "video/*")) {
extUTI = "public.movie" as CFString;
} else {
extUTI = kUTTypeData

if mimeType.range(of: "*") == nil {
let utiUnmanaged = UTTypeCreatePreferredIdentifierForTag(
kUTTagClassMIMEType,
mimeType as CFString,
nil
)

if let uti = (utiUnmanaged?.takeRetainedValue() as String?) {
if !uti.hasPrefix("dyn.") {
extUTI = uti as CFString
}
}
break
}
}
}

extUTIs.append(extUTI! as String)
}

DispatchQueue.main.async {
let types: [String] = extUTIs
let documentPicker = UIDocumentPickerViewController(documentTypes: types, in: .import)
documentPicker.delegate = self
documentPicker.modalPresentationStyle = .formSheet
documentPicker.allowsMultipleSelection = multiple
if (extUTIs.count == 1 && extUTIs.contains("public.movie")){
let videoPicker = UIImagePickerController()
videoPicker.delegate = self
videoPicker.sourceType = .photoLibrary
videoPicker.mediaTypes = [kUTTypeMovie as String]

self.bridge!.viewController!.present(documentPicker, animated: true)
self.bridge!.viewController!.present(videoPicker, animated: true)
} else {
let documentPicker = UIDocumentPickerViewController(documentTypes: extUTIs, in: .import)
documentPicker.delegate = self
documentPicker.modalPresentationStyle = .formSheet
documentPicker.allowsMultipleSelection = multiple

self.bridge!.viewController!.present(documentPicker, animated: true)
}
}
}
}
Expand All @@ -71,12 +77,12 @@ extension FilePickerPlugin: UIDocumentPickerDelegate {
for url in urls
{
var file = JSObject()

file["path"] = url.absoluteString
file["webPath"] = self.bridge?.portablePath(fromLocalURL: url)?.absoluteString
file["name"] = url.lastPathComponent
file["extension"] = url.pathExtension

files.append(file);
}

Expand All @@ -89,3 +95,57 @@ extension FilePickerPlugin: UIDocumentPickerDelegate {
savedCall!.reject("canceled")
}
}

extension FilePickerPlugin: UIImagePickerControllerDelegate {
public func imagePickerController(_ controller: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard
let mediaUrl = info[.mediaURL] as? URL
else {
savedCall!.reject("Cannot get video URL")
return
}

var url: URL;
do{
url = try saveTemporaryVideo(mediaUrl)
} catch{
savedCall!.reject("Cannot save video")
return
}

var files = [JSObject]()
var file = JSObject()

file["path"] = url.absoluteString
file["webPath"] = self.bridge?.portablePath(fromLocalURL: url)?.absoluteString
file["name"] = url.lastPathComponent
file["extension"] = url.pathExtension

files.append(file);

controller.dismiss(animated: true) {
self.savedCall!.resolve([
"files": files
])
}
}

public func imagePickerControllerDidCancel(_ controller: UIImagePickerController) {
controller.dismiss(animated: true) {
self.savedCall!.reject("canceled")
}
}

func saveTemporaryVideo(_ mediaUrl: URL) throws -> URL {
let url = URL(fileURLWithPath: NSTemporaryDirectory())
.appendingPathComponent(mediaUrl.lastPathComponent)

try FileManager.default.copyItem(at: mediaUrl, to: url)

return url
}
}

extension FilePickerPlugin: UINavigationControllerDelegate {

}

0 comments on commit c550c77

Please sign in to comment.