99
1010@implementation FilePicker
1111
12- - (void )isAvailable : (CDVInvokedUrlCommand*)command {
13- BOOL supported = NSClassFromString (@" UIDocumentPickerViewController" );
12+ - (void )isAvailable : (CDVInvokedUrlCommand*)command
13+ {
14+ BOOL supported = [UIDocumentPickerViewController class ] != nil ;
1415 [self .commandDelegate sendPluginResult: [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsBool: supported] callbackId: command.callbackId];
1516}
1617
17- - (void )pickFile : (CDVInvokedUrlCommand*)command {
18-
18+ - (void )pickFile : (CDVInvokedUrlCommand*)command
19+ {
1920 self.command = command;
2021 id UTIs = [command.arguments objectAtIndex: 0 ];
2122 BOOL supported = YES ;
@@ -46,7 +47,7 @@ - (void)pickFile:(CDVInvokedUrlCommand*)command {
4647 [self .commandDelegate sendPluginResult: [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString: @" not supported" ] callbackId: self .command.callbackId];
4748 }
4849
49- if (!NSClassFromString ( @" UIDocumentPickerViewController" ) ) {
50+ if (![ UIDocumentPickerViewController class ] ) {
5051 supported = NO ;
5152 [self .commandDelegate sendPluginResult: [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString: @" your device can't show the file picker" ] callbackId: self .command.callbackId];
5253 }
@@ -56,24 +57,22 @@ - (void)pickFile:(CDVInvokedUrlCommand*)command {
5657 [self .pluginResult setKeepCallbackAsBool: YES ];
5758 [self displayDocumentPicker: UTIsArray withSenderRect: frame];
5859 }
59-
6060}
6161
6262#pragma mark - UIDocumentMenuDelegate
63- -(void )documentMenu : (UIDocumentMenuViewController *)documentMenu didPickDocumentPicker : (UIDocumentPickerViewController *)documentPicker {
64-
63+ -(void )documentMenu : (UIDocumentMenuViewController *)documentMenu didPickDocumentPicker : (UIDocumentPickerViewController *)documentPicker
64+ {
6565 documentPicker.delegate = self;
6666 documentPicker.modalPresentationStyle = UIModalPresentationFullScreen;
6767 [self .viewController presentViewController: documentPicker animated: YES completion: nil ];
68-
6968}
7069
71- -(void )documentMenuWasCancelled : (UIDocumentMenuViewController *)documentMenu {
72-
70+ -(void )documentMenuWasCancelled : (UIDocumentMenuViewController *)documentMenu
71+ {
72+
7373 self.pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString: @" canceled" ];
7474 [self .pluginResult setKeepCallbackAsBool: NO ];
7575 [self .commandDelegate sendPluginResult: self .pluginResult callbackId: self .command.callbackId];
76-
7776}
7877
7978#pragma mark - UIDocumentPickerDelegate
@@ -84,24 +83,30 @@ - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocum
8483 [self .commandDelegate sendPluginResult: self .pluginResult callbackId: self .command.callbackId];
8584
8685}
87- - (void )documentPickerWasCancelled : (UIDocumentPickerViewController *)controller {
8886
87+ - (void )documentPickerWasCancelled : (UIDocumentPickerViewController *)controller
88+ {
8989 self.pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString: @" canceled" ];
9090 [self .pluginResult setKeepCallbackAsBool: NO ];
9191 [self .commandDelegate sendPluginResult: self .pluginResult callbackId: self .command.callbackId];
92-
9392}
9493
95- - (void )displayDocumentPicker : (NSArray *)UTIs withSenderRect : (CGRect)senderFrame {
96-
97- UIDocumentMenuViewController *importMenu = [[UIDocumentMenuViewController alloc ] initWithDocumentTypes: UTIs inMode: UIDocumentPickerModeImport];
98- importMenu.delegate = self;
99- importMenu.popoverPresentationController .sourceView = self.viewController .view ;
100- if (!CGRectEqualToRect (senderFrame, CGRectZero)) {
101- importMenu.popoverPresentationController .sourceRect = senderFrame;
94+ - (void )displayDocumentPicker : (NSArray *)UTIs withSenderRect : (CGRect)senderFrame
95+ {
96+ UIViewController * vc = nil ;
97+ if (!IsAtLeastiOSVersion (@" 11.0" )) {
98+ vc = [[UIDocumentMenuViewController alloc ] initWithDocumentTypes: UTIs inMode: UIDocumentPickerModeImport];
99+ ((UIDocumentMenuViewController *)vc).delegate = self;
100+ vc.popoverPresentationController .sourceView = self.viewController .view ;
101+ if (!CGRectEqualToRect (senderFrame, CGRectZero)) {
102+ vc.popoverPresentationController .sourceRect = senderFrame;
103+ }
104+ } else {
105+ vc = [[UIDocumentPickerViewController alloc ] initWithDocumentTypes: UTIs inMode: UIDocumentPickerModeImport];
106+ ((UIDocumentPickerViewController *)vc).delegate = self;
107+ vc.modalPresentationStyle = UIModalPresentationFullScreen;
102108 }
103- [self .viewController presentViewController: importMenu animated: YES completion: nil ];
104-
109+ [self .viewController presentViewController: vc animated: YES completion: nil ];
105110}
106111
107112@end
0 commit comments