Skip to content

Commit

Permalink
feat: add explicit pickDirectory() ios implementation (#483)
Browse files Browse the repository at this point in the history
  • Loading branch information
vonovak authored Oct 6, 2021
1 parent d3e5632 commit 45f57bc
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Requires RN >= 0.63, Android 5.0+ and iOS 11+
- [RN >= 0.63](#rn--063)
- [API](#api)
- [DocumentPicker.pickMultiple(options) / DocumentPicker.pickSingle(options) / DocumentPicker.pick(options)](#documentpickerpickmultipleoptions--documentpickerpicksingleoptions--documentpickerpickoptions)
- [[Android and Windows only] DocumentPicker.pickDirectory()](#android-and-windows-only-documentpickerpickdirectory)
- [DocumentPicker.pickDirectory()](#documentpickerpickdirectory)
- [DocumentPicker.pick(options) and DocumentPicker.pickMultiple(options)](#documentpickerpickoptions-and-documentpickerpickmultipleoptions)
- [Options](#options)
- [allowMultiSelection:boolean](#allowmultiselectionboolean)
Expand Down Expand Up @@ -73,7 +73,7 @@ If you are using RN >= 0.63, only run `pod install` from the ios directory. Then

Use `pickMultiple`, `pickSingle` or `pick` to open a document picker for the user to select file(s). All methods return a Promise.

#### [Android and Windows only] `DocumentPicker.pickDirectory()`
#### `DocumentPicker.pickDirectory()`

Open a system directory picker. Returns a promise that resolves to (`{ uri: string }`) of the directory selected by user.

Expand Down
2 changes: 1 addition & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export default function App() {
}}
/>
<Button
title="open directory picker (android+windows only)"
title="open directory picker"
onPress={() => {
DocumentPicker.pickDirectory().then(setResult).catch(handleError)
}}
Expand Down
10 changes: 7 additions & 3 deletions ios/RNDocumentPicker.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#import <React/RCTUtils.h>
#import "RNCPromiseWrapper.h"


static NSString *const E_DOCUMENT_PICKER_CANCELED = @"DOCUMENT_PICKER_CANCELED";
static NSString *const E_INVALID_DATA_RETURNED = @"INVALID_DATA_RETURNED";

Expand Down Expand Up @@ -87,7 +88,8 @@ - (dispatch_queue_t)methodQueue
[promiseWrapper setPromiseWithInProgressCheck:resolve rejecter:reject fromCallSite:@"pick"];

NSArray *allowedUTIs = [RCTConvert NSArray:options[OPTION_TYPE]];
UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:(NSArray *)allowedUTIs inMode:mode];
UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:allowedUTIs inMode:mode];

documentPicker.modalPresentationStyle = presentationStyle;

documentPicker.delegate = self;
Expand Down Expand Up @@ -126,6 +128,7 @@ - (NSMutableDictionary *)getMetadataForUrl:(NSURL *)url error:(NSError **)error
[urlsInOpenMode addObject:url];
}

// TODO handle error
[url startAccessingSecurityScopedResource];

NSFileCoordinator *coordinator = [NSFileCoordinator new];
Expand Down Expand Up @@ -158,7 +161,7 @@ - (NSMutableDictionary *)getMetadataForUrl:(NSURL *)url error:(NSError **)error
}

if (newURL.pathExtension != nil) {
CFStringRef extension = (__bridge CFStringRef)[newURL pathExtension];
CFStringRef extension = (__bridge CFStringRef) newURL.pathExtension;
CFStringRef uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, extension, NULL);
CFStringRef mimeType = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType);
if (uti) {
Expand Down Expand Up @@ -238,7 +241,8 @@ - (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller
[self rejectAsUserCancellationError];
}

- (void)presentationControllerDidDismiss:(UIPresentationController *)presentationController {
- (void)presentationControllerDidDismiss:(UIPresentationController *)presentationController
{
[self rejectAsUserCancellationError];
}

Expand Down
17 changes: 12 additions & 5 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,19 @@ export type DocumentPickerOptions<OS extends SupportedPlatforms> = {
allowMultiSelection?: boolean
} & Pick<ModalPropsIOS, 'presentationStyle'>

export function pickDirectory(): Promise<DirectoryPickerResponse | null> {
if (Platform.OS === 'android' || Platform.OS === 'windows') {
return RNDocumentPicker.pickDirectory()
export async function pickDirectory<OS extends SupportedPlatforms>(
params?: Pick<DocumentPickerOptions<OS>, 'presentationStyle'>,
): Promise<DirectoryPickerResponse | null> {
if (Platform.OS === 'ios') {
const result = await pick({
...params,
mode: 'open',
allowMultiSelection: false,
type: ['public.folder'],
})
return { uri: result[0].uri }
} else {
// TODO iOS impl
return Promise.resolve(null)
return RNDocumentPicker.pickDirectory()
}
}

Expand Down

0 comments on commit 45f57bc

Please sign in to comment.