@@ -26,69 +26,73 @@ class TUTFileChooser: NSObject, UIImagePickerControllerDelegate, UINavigationCon
26
26
imagePickerController. delegate = self
27
27
}
28
28
29
- @MainActor public func open( withAnchorRect anchorRect: CGRect ) async throws -> [ String ] {
29
+ @MainActor public func open( withAnchorRect anchorRect: CGRect , isFileOnly : Bool ) async throws -> [ String ] {
30
30
if let previousHandler = resultHandler {
31
31
TUTSLog ( " Another file picker is already open? " )
32
32
sourceController. dismiss ( animated: true , completion: nil )
33
33
previousHandler ( . success( [ ] ) )
34
34
}
35
35
36
- let attachmentTypeMenu = UIDocumentMenuViewController ( documentTypes: supportedUTIs, in: UIDocumentPickerMode . import)
37
-
38
- self . attachmentTypeMenu = attachmentTypeMenu
39
-
40
- attachmentTypeMenu. delegate = self
36
+ var filePicker : UIDocumentPickerViewController ?
37
+ if isFileOnly {
38
+ filePicker = UIDocumentPickerViewController ( forOpeningContentTypes: [ UTType . content, UTType . archive, UTType . data] , asCopy: true )
39
+ filePicker!. delegate = self
40
+ } else {
41
+ self . attachmentTypeMenu = UIDocumentMenuViewController ( documentTypes: supportedUTIs, in: UIDocumentPickerMode . import)
42
+ self . attachmentTypeMenu!. delegate = self
43
+ }
41
44
42
45
// add menu item for selecting images from photo library.
43
46
// according to developer documentation check if the source type is available first https://developer.apple.com/reference/uikit/uiimagepickercontroller
44
- if UIImagePickerController . isSourceTypeAvailable ( . savedPhotosAlbum) {
47
+ if !isFileOnly && UIImagePickerController . isSourceTypeAvailable ( . savedPhotosAlbum) {
45
48
if UIDevice . current. userInterfaceIdiom == UIUserInterfaceIdiom . pad {
46
- attachmentTypeMenu . modalPresentationStyle = . popover
47
- popOverPresentationController = attachmentTypeMenu . popoverPresentationController
49
+ filePicker ? . modalPresentationStyle = . popover
50
+ popOverPresentationController = filePicker ? . popoverPresentationController
48
51
popOverPresentationController? . permittedArrowDirections = [ . up, . down]
49
52
popOverPresentationController? . sourceView = sourceController. view
50
53
popOverPresentationController? . sourceRect = anchorRect
51
54
}
52
55
let photosLabel = translate ( " TutaoChoosePhotosAction " , default: " Photos " )
53
- attachmentTypeMenu. addOption (
54
- withTitle: photosLabel,
55
- image: photoLibImage,
56
- order: . first,
57
- handler: { [ weak self] in
58
- // capture the weak reference to avoid reference self
59
- guard let self else { return }
60
-
61
- // No need to ask for permissions with new picker
62
- if #available( iOS 14 . 0 , * ) {
63
- self . showPhpicker ( anchor: anchorRect)
64
- } else {
65
- // ask for permission because of changed behaviour in iOS 11
66
- if PHPhotoLibrary . authorizationStatus ( ) == . notDetermined {
67
- PHPhotoLibrary . requestAuthorization ( { status in
68
- if status == . authorized { self . showLegacyImagePicker ( anchor: anchorRect) } else { self . sendResult ( filePath: nil ) }
69
- } )
70
- } else if PHPhotoLibrary . authorizationStatus ( ) == . authorized {
71
- self . showLegacyImagePicker ( anchor: anchorRect)
56
+ self . attachmentTypeMenu!
57
+ . addOption (
58
+ withTitle: photosLabel,
59
+ image: photoLibImage,
60
+ order: . first,
61
+ handler: { [ weak self] in
62
+ // capture the weak reference to avoid reference self
63
+ guard let self else { return }
64
+
65
+ // No need to ask for permissions with new picker
66
+ if #available( iOS 14 . 0 , * ) {
67
+ self . showPhpicker ( anchor: anchorRect)
72
68
} else {
73
- self . showPermissionDeniedDialog ( )
69
+ // ask for permission because of changed behaviour in iOS 11
70
+ if PHPhotoLibrary . authorizationStatus ( ) == . notDetermined {
71
+ PHPhotoLibrary . requestAuthorization ( { status in
72
+ if status == . authorized { self . showLegacyImagePicker ( anchor: anchorRect) } else { self . sendResult ( filePath: nil ) }
73
+ } )
74
+ } else if PHPhotoLibrary . authorizationStatus ( ) == . authorized {
75
+ self . showLegacyImagePicker ( anchor: anchorRect)
76
+ } else {
77
+ self . showPermissionDeniedDialog ( )
78
+ }
74
79
}
75
80
}
76
- }
77
- )
81
+ )
78
82
}
79
83
80
84
// add menu item for opening the camera and take a photo or video.
81
85
// according to developer documentation check if the source type is available first https://developer.apple.com/reference/uikit/uiimagepickercontroller
82
- if UIImagePickerController . isSourceTypeAvailable ( . camera) {
86
+ if !isFileOnly && UIImagePickerController . isSourceTypeAvailable ( . camera) {
83
87
let cameraLabel = translate ( " TutaoShowCameraAction " , default: " Camera " )
84
88
85
89
// capture the weak reference to avoid reference cycle
86
- attachmentTypeMenu. addOption ( withTitle: cameraLabel, image: cameraImage, order: . first) { [ weak self] in self ? . openCamera ( ) }
90
+ self . attachmentTypeMenu! . addOption ( withTitle: cameraLabel, image: cameraImage, order: . first) { [ weak self] in self ? . openCamera ( ) }
87
91
}
88
92
89
93
return try await withCheckedThrowingContinuation { continuation in
90
94
resultHandler = continuation. resume ( with: )
91
- sourceController. present ( attachmentTypeMenu, animated: true , completion: nil )
95
+ sourceController. present ( ( isFileOnly ? filePicker : self . attachmentTypeMenu) ! , animated: true , completion: nil )
92
96
}
93
97
}
94
98
@@ -311,18 +315,18 @@ class TUTFileChooser: NSObject, UIImagePickerControllerDelegate, UINavigationCon
311
315
}
312
316
313
317
/**
314
- * Replace ".heic" or ".heif" extensions with ".jpeg".
315
- */
318
+ * Replace ".heic" or ".heif" extensions with ".jpeg".
319
+ */
316
320
private func changeExtensionToJpeg( filename: URL ) -> URL { filename. deletingPathExtension ( ) . appendingPathExtension ( " jpg " ) }
317
321
}
318
322
319
323
/**
320
- Extending TUTFileChooser on iOS14 to conform to ickerViewControllerDelegate
324
+ Extending TUTFileChooser on iOS14 to conform to ickerViewControllerDelegate
321
325
*/
322
326
@available ( iOS 14 . 0 , * ) extension TUTFileChooser : PHPickerViewControllerDelegate {
323
327
/**
324
- Invoked when user finished picking the files.
325
- */
328
+ Invoked when user finished picking the files.
329
+ */
326
330
func picker( _ picker: PHPickerViewController , didFinishPicking results: [ PHPickerResult ] ) {
327
331
picker. dismiss ( animated: true , completion: nil )
328
332
0 commit comments