comparison ios/dw.m @ 2712:ada3ac4677f6

iOS: Add file browser support for selecting folders/directories. Switch to using UTType on iOS 14 and above. Still getting deprecation warnings on code in the iOS 13 section.... I thought @available(iOS 14, *) would silence those warnings?
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Wed, 01 Dec 2021 22:37:22 +0000
parents 00e42b9c9ebc
children 2987bf90c7c9
comparison
equal deleted inserted replaced
2711:00e42b9c9ebc 2712:ada3ac4677f6
11 #import <Foundation/Foundation.h> 11 #import <Foundation/Foundation.h>
12 #import <UIKit/UIKit.h> 12 #import <UIKit/UIKit.h>
13 #import <WebKit/WebKit.h> 13 #import <WebKit/WebKit.h>
14 #import <AudioToolbox/AudioToolbox.h> 14 #import <AudioToolbox/AudioToolbox.h>
15 #import <UserNotifications/UserNotifications.h> 15 #import <UserNotifications/UserNotifications.h>
16 #import <UniformTypeIdentifiers/UTDefines.h>
17 #import <UniformTypeIdentifiers/UTType.h>
18 #import <UniformTypeIdentifiers/UTCoreTypes.h>
16 #include "dw.h" 19 #include "dw.h"
17 #include <sys/utsname.h> 20 #include <sys/utsname.h>
18 #include <sys/socket.h> 21 #include <sys/socket.h>
19 #include <sys/un.h> 22 #include <sys/un.h>
20 #include <sys/mman.h> 23 #include <sys/mman.h>
1112 [delegate release]; 1115 [delegate release];
1113 } 1116 }
1114 -(void)filePicker:(NSPointerArray *)params 1117 -(void)filePicker:(NSPointerArray *)params
1115 { 1118 {
1116 DWDialog *dialog = dw_dialog_new(NULL); 1119 DWDialog *dialog = dw_dialog_new(NULL);
1117 UIDocumentPickerViewController *picker ; 1120 UIDocumentPickerViewController *picker;
1118 DWDocumentPickerDelegate *delegate = [[DWDocumentPickerDelegate alloc] init]; 1121 DWDocumentPickerDelegate *delegate = [[DWDocumentPickerDelegate alloc] init];
1119 UIDocumentPickerMode mode = UIDocumentPickerModeOpen;
1120 char *defpath = [params pointerAtIndex:0]; 1122 char *defpath = [params pointerAtIndex:0];
1121 char *ext = [params pointerAtIndex:1]; 1123 char *ext = [params pointerAtIndex:1];
1122 int flags = DW_POINTER_TO_INT([params pointerAtIndex:2]); 1124 int flags = DW_POINTER_TO_INT([params pointerAtIndex:2]);
1123 char *file = NULL; 1125 char *file = NULL;
1124 NSArray *UTIs; 1126 NSArray *UTIs;
1125 1127
1126 /* Setup the picker */ 1128 if (@available(iOS 14, *)) {
1127 if(flags & DW_FILE_SAVE) 1129 UTType *extuti = ext ? [UTType typeWithFilenameExtension:[NSString stringWithUTF8String:ext]] : nil;
1128 mode = UIDocumentPickerModeExportToService; 1130
1129 /* Try to generate a UTI for our passed extension */ 1131 /* Try to generate a UTI for our passed extension */
1130 if(ext) 1132 if(flags & DW_DIRECTORY_OPEN)
1131 UTIs = [NSArray arrayWithObjects:[NSString stringWithFormat:@"public.%s", ext], @"public.text", nil]; 1133 UTIs = @[UTTypeFolder];
1132 else 1134 else
1133 UTIs = @[@"public.text"]; 1135 {
1134 picker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:UTIs inMode:mode]; 1136 if(extuti)
1137 UTIs = [NSArray arrayWithObjects:extuti, UTTypeText, nil];
1138 else
1139 UTIs = @[UTTypeText];
1140 }
1141 picker = [[UIDocumentPickerViewController alloc] initForOpeningContentTypes:UTIs
1142 asCopy:(flags & DW_FILE_SAVE ? YES: NO)];
1143 } else {
1144 UIDocumentPickerMode mode = UIDocumentPickerModeOpen;
1145
1146 /* Try to generate a UTI for our passed extension */
1147 if(flags & DW_DIRECTORY_OPEN)
1148 UTIs = @[@"public.directory"];
1149 else
1150 {
1151 if(ext)
1152 UTIs = [NSArray arrayWithObjects:[NSString stringWithFormat:@"public.%s", ext], @"public.text", nil];
1153 else
1154 UTIs = @[@"public.text"];
1155 }
1156
1157 /* Setup the picker */
1158 if(flags & DW_FILE_SAVE)
1159 mode = UIDocumentPickerModeExportToService;
1160
1161 picker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:UTIs inMode:mode];
1162 }
1135 [picker setAllowsMultipleSelection:NO]; 1163 [picker setAllowsMultipleSelection:NO];
1136 [picker setShouldShowFileExtensions:YES]; 1164 [picker setShouldShowFileExtensions:YES];
1137 if(defpath) 1165 if(defpath)
1138 [picker setDirectoryURL:[NSURL URLWithString:[NSString stringWithUTF8String:defpath]]]; 1166 [picker setDirectoryURL:[NSURL URLWithString:[NSString stringWithUTF8String:defpath]]];
1139 /* Unhide our hidden window and make it key */ 1167 /* Unhide our hidden window and make it key */