From a374b027a0e3f0891b244284ed1aed10e6691503 Mon Sep 17 00:00:00 2001 From: Aleko Petkov Date: Mon, 15 Nov 2021 14:28:07 -0800 Subject: [PATCH 1/2] Update file chooser plugin to be more consistent across platforms: - When canceled, both versions reject with "RESULT_CANCELED". The docs say the plugin resolves with undefined, but that's a bigger change. - iOS now returns an array of files, like Android. - The getFile method has been renamed to getFiles to reflect the fact that the plugin returns multiple files. - Updated README. - Bumped major version due to breaking API changes. --- README.md | 6 +++--- package.json | 4 ++-- plugin.xml | 2 +- src/android/Chooser.java | 6 +++--- src/ios/Chooser.swift | 14 +++++++------- www/chooser.js | 4 ++-- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 7002bde..269b9fd 100755 --- a/README.md +++ b/README.md @@ -19,17 +19,17 @@ cordova plugin add cordova-plugin-simple-file-chooser ```js /** - * Displays native prompt for user to select a file. + * Displays native prompt for user to select one or more files. * * @param accept Optional MIME type filter (e.g. 'image/gif,video/*'). * - * @returns Promise containing selected file's information, + * @returns Promise containing selected files' information, * MIME type, display name, and original URI. * * If user cancels, promise will be resolved as undefined. * If error occurs, promise will be rejected. */ -chooser.getFile(accept?: string) : Promise Chooser Cyph, Inc. diff --git a/src/android/Chooser.java b/src/android/Chooser.java index 5ec7347..ce031ba 100755 --- a/src/android/Chooser.java +++ b/src/android/Chooser.java @@ -23,7 +23,7 @@ import org.json.JSONObject; public class Chooser extends CordovaPlugin { - private static final String ACTION_OPEN = "getFile"; + private static final String ACTION_OPEN = "getFiles"; private static final int PICK_FILE_REQUEST = 1; private static final String TAG = "Chooser"; @@ -95,7 +95,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { this.callback.error("File URI was null."); } } else if (resultCode == Activity.RESULT_CANCELED) { - this.callback.error("No File selected."); + this.callback.error("RESULT_CANCELED"); } else { this.callback.error(resultCode); } @@ -122,4 +122,4 @@ public JSONObject processFileUri(Uri uri) { } return file; } -} +} \ No newline at end of file diff --git a/src/ios/Chooser.swift b/src/ios/Chooser.swift index 89c90c1..1d4563f 100755 --- a/src/ios/Chooser.swift +++ b/src/ios/Chooser.swift @@ -1,7 +1,7 @@ import UIKit import MobileCoreServices import Foundation - +import Cordova @objc(Chooser) class Chooser : CDVPlugin { @@ -46,12 +46,12 @@ class Chooser : CDVPlugin { } do { - let result = [ + let result = [[ "data": data.base64EncodedString(), "mediaType": self.detectMimeType(newURL), "name": newURL.lastPathComponent, "uri": newURL.absoluteString - ] + ]] if let message = try String( data: JSONSerialization.data( @@ -80,8 +80,8 @@ class Chooser : CDVPlugin { url.stopAccessingSecurityScopedResource() } - @objc(getFile:) - func getFile (command: CDVInvokedUrlCommand) { + @objc(getFiles:) + func getFiles(command: CDVInvokedUrlCommand) { self.commandCallback = command.callbackId let accept = command.arguments.first as! String @@ -163,6 +163,6 @@ extension Chooser : UIDocumentPickerDelegate { } func documentPickerWasCancelled (_ controller: UIDocumentPickerViewController) { - self.send("RESULT_CANCELED") + self.sendError("RESULT_CANCELED") } -} +} \ No newline at end of file diff --git a/www/chooser.js b/www/chooser.js index adafa30..41d3979 100755 --- a/www/chooser.js +++ b/www/chooser.js @@ -1,5 +1,5 @@ module.exports = { - getFile: function (accept, successCallback, failureCallback) { + getFiles: function (accept, successCallback, failureCallback) { var result = new Promise(function (resolve, reject) { cordova.exec( function (json) { @@ -12,7 +12,7 @@ module.exports = { }, reject, 'Chooser', - 'getFile', + 'getFiles', [(typeof accept === 'string' ? accept.replace(/\s/g, '') : undefined) || '*/*'] ); }); From 38754c23d69a45ebe3e1c34320fc30416a29a8f5 Mon Sep 17 00:00:00 2001 From: Harsh Zalavadiya Date: Tue, 16 Nov 2021 13:11:57 +0530 Subject: [PATCH 2/2] Update Chooser.java --- src/android/Chooser.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/android/Chooser.java b/src/android/Chooser.java index ce031ba..a227b42 100755 --- a/src/android/Chooser.java +++ b/src/android/Chooser.java @@ -122,4 +122,4 @@ public JSONObject processFileUri(Uri uri) { } return file; } -} \ No newline at end of file +}