Skip to content
This repository has been archived by the owner on Apr 1, 2023. It is now read-only.

Commit

Permalink
improve function naming, fix empty types fallback, fix fallback to st…
Browse files Browse the repository at this point in the history
…artAccessingSecurityScopedResource
  • Loading branch information
GabrielNSD committed Oct 25, 2022
1 parent 86e5160 commit e31b1e2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ Open the file picker that allows the user to select one or more files.

#### PickFilesOptions

| Prop | Type | Description |
| -------------- | --------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **`types`** | <code>string[]</code> | List of accepted file types. Look at [IANA Media Types](https://www.iana.org/assignments/media-types/media-types.xhtml) for a complete list of standard media types. This option cannot be used with `multiple: true` on Android. Example: `['image/png', 'application/pdf']` |
| **`customExtensions`** | <code>string[]</code> | List of custom extensions. Necessary in iOS since the mimetype alone is not enought Example: `['cs2']` |
| **`multiple`** | <code>boolean</code> | Whether multiple files may be selected. Default: `false` |
| **`readData`** | <code>boolean</code> | Whether to read the file data. Default: `false` |
| Prop | Type | Description |
| ---------------------- | --------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **`types`** | <code>string[]</code> | List of accepted file types. Look at [IANA Media Types](https://www.iana.org/assignments/media-types/media-types.xhtml) for a complete list of standard media types. This option cannot be used with `multiple: true` on Android. Example: `['image/png', 'application/pdf']` |
| **`customExtensions`** | <code>string[]</code> | iOS only. List of custom file name extensions (without leading dot '.'). Necessary in iOS since custom mimetypes are not supported. Example: `['cs2']` |
| **`multiple`** | <code>boolean</code> | Whether multiple files may be selected. Default: `false` |
| **`readData`** | <code>boolean</code> | Whether to read the file data. Default: `false` |

</docgen-api>

Expand Down
2 changes: 1 addition & 1 deletion ios/Plugin/FilePicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import UniformTypeIdentifiers
}

@available(iOS 14.0, *)
public func updatedOpenDocumentPicker(multiple: Bool, documentTypes: [UTType]) {
public func openDocumentPickerWithFileExtensions(multiple: Bool, documentTypes: [UTType]) {
DispatchQueue.main.async {
let documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: documentTypes)
documentPicker.delegate = self
Expand Down
19 changes: 10 additions & 9 deletions ios/Plugin/FilePickerPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ public class FilePickerPlugin: CAPPlugin {
let types = call.getArray("types", String.self) ?? []
let fileExtensions = call.getArray("customExtensions", String.self) ?? []
if #available(iOS 14.0, *) {
let parsedTypes = newParseTypesOption(types)
let parsedTypes = parseTypesOptionsToUTTypes(types)
let parsedExtensions = parseCustomExtensions(fileExtensions)
let concatenatedTypes = parsedTypes + parsedExtensions
let documentTypes = concatenatedTypes.isEmpty ? [.jpeg] : concatenatedTypes
implementation?.updatedOpenDocumentPicker(multiple: multiple, documentTypes: documentTypes)
let documentTypes = concatenatedTypes.isEmpty ? [.data] : concatenatedTypes
implementation?.openDocumentPickerWithFileExtensions(multiple: multiple, documentTypes: documentTypes)
} else {
// Fallback on earlier versions
let parsedTypes = parseTypesOption(types)
Expand All @@ -51,7 +51,7 @@ public class FilePickerPlugin: CAPPlugin {
}

@available(iOS 14.0, *)
@objc func newParseTypesOption(_ types: [String]) -> [UTType] {
@objc func parseTypesOptionsToUTTypes(_ types: [String]) -> [UTType] {
var parsedTypes: [UTType] = []
for (_, type) in types.enumerated() {
guard let utType: UTType = UTType(mimeType: type) else {
Expand Down Expand Up @@ -84,15 +84,16 @@ public class FilePickerPlugin: CAPPlugin {
return
}
for (url) in urls {
guard url.startAccessingSecurityScopedResource() else {
return
}
}
guard url.startAccessingSecurityScopedResource() else {
return
}
}
do {
var result = JSObject()
let filesResult = try urls.map {(url: URL) -> JSObject in
guard url.startAccessingSecurityScopedResource() else {
return
let error = NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey: "File access denied"])
throw error
}
var file = JSObject()
file["path"] = implementation?.getPathFromUrl(url) ?? ""
Expand Down

0 comments on commit e31b1e2

Please sign in to comment.