Skip to content

Commit

Permalink
Merge pull request #985 from noviocare/AM_conditional
Browse files Browse the repository at this point in the history
Add conditional compilation of media, audio and document pickers for iOS
  • Loading branch information
Miguel Ruivo authored May 26, 2022
2 parents 443808b + be27522 commit 5b9dc6e
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 21 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 4.6.0

#### iOS
Add conditional compilation of media, audio and document pickers for iOS.
This prevents error messages for permissions (NSPhotoLibraryUsageDescription, NSAppleMusicUsageDescription, etc.) when publishing to app store connect, in case you don't need either category. This addresses [#783](https://github.com/miguelpruivo/flutter_file_picker/issues/783) in a different way.

## 4.5.1

#### Web
Expand Down
15 changes: 12 additions & 3 deletions ios/Classes/FilePickerPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@
#import <Photos/Photos.h>
#import <MobileCoreServices/MobileCoreServices.h>

#if __has_include(<PhotosUI/PHPicker.h>) || __has_include("PHPicker.h")
#if PICKER_MEDIA && (__has_include(<PhotosUI/PHPicker.h>) || __has_include("PHPicker.h"))
#define PHPicker
#import <PhotosUI/PHPicker.h>
#endif

@interface FilePickerPlugin : NSObject<FlutterPlugin, FlutterStreamHandler, UIAdaptivePresentationControllerDelegate, UIDocumentPickerDelegate, UITabBarDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, MPMediaPickerControllerDelegate
@interface FilePickerPlugin : NSObject<FlutterPlugin, FlutterStreamHandler, UITabBarDelegate, UINavigationControllerDelegate, UIAdaptivePresentationControllerDelegate
#ifdef PICKER_MEDIA
, UIImagePickerControllerDelegate
#ifdef PHPicker
, PHPickerViewControllerDelegate
, PHPickerViewControllerDelegate
#endif
#endif // PICKER_MEDIA
#ifdef PICKER_DOCUMENT
, UIDocumentPickerDelegate
#endif // PICKER_DOCUMENT
#ifdef PICKER_AUDIO
, MPMediaPickerControllerDelegate
#endif // PICKER_AUDIO
>
@end
60 changes: 56 additions & 4 deletions ios/Classes/FilePickerPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@
#import "FileUtils.h"
#import "ImageUtils.h"

#ifdef PICKER_MEDIA
@import DKImagePickerController;

@interface FilePickerPlugin() <DKImageAssetExporterObserver>
#else
@interface FilePickerPlugin()
#endif
@property (nonatomic) FlutterResult result;
@property (nonatomic) FlutterEventSink eventSink;
#ifdef PICKER_MEDIA
@property (nonatomic) UIImagePickerController *galleryPickerController;
#endif
#ifdef PICKER_DOCUMENT
@property (nonatomic) UIDocumentPickerViewController *documentPickerController;
@property (nonatomic) UIDocumentInteractionController *interactionController;
#endif
@property (nonatomic) MPMediaPickerController *audioPickerController;
@property (nonatomic) NSArray<NSString *> * allowedExtensions;
@property (nonatomic) BOOL loadDataToMemory;
Expand Down Expand Up @@ -87,7 +95,13 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {

if([call.method isEqualToString:@"dir"]) {
if (@available(iOS 13, *)) {
#ifdef PICKER_DOCUMENT
[self resolvePickDocumentWithMultiPick:NO pickDirectory:YES];
#else
_result([FlutterError errorWithCode:@"Unsupported picker type"
message:@"Support for the Document picker is not compiled in. Remove the Pod::PICKER_DOCUMENT=false statement from your Podfile."
details:nil]);
#endif
} else {
_result([self getDocumentDirectory]);
_result = nil;
Expand All @@ -109,12 +123,30 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
details:nil]);
_result = nil;
} else if(self.allowedExtensions != nil) {
#ifdef PICKER_DOCUMENT
[self resolvePickDocumentWithMultiPick:isMultiplePick pickDirectory:NO];
#else
_result([FlutterError errorWithCode:@"Unsupported picker type"
message:@"Support for the Document picker is not compiled in. Remove the Pod::PICKER_DOCUMENT=false statement from your Podfile."
details:nil]);
#endif
}
} else if([call.method isEqualToString:@"video"] || [call.method isEqualToString:@"image"] || [call.method isEqualToString:@"media"]) {
#ifdef PICKER_MEDIA
[self resolvePickMedia:[FileUtils resolveMediaType:call.method] withMultiPick:isMultiplePick withCompressionAllowed:self.allowCompression];
#else
_result([FlutterError errorWithCode:@"Unsupported picker type"
message:@"Support for the Media picker is not compiled in. Remove the Pod::PICKER_MEDIA=false statement from your Podfile."
details:nil]);
#endif
} else if([call.method isEqualToString:@"audio"]) {
[self resolvePickAudioWithMultiPick: isMultiplePick];
#ifdef PICKER_AUDIO
[self resolvePickAudioWithMultiPick: isMultiplePick];
#else
_result([FlutterError errorWithCode:@"Unsupported picker type"
message:@"Support for the Audio picker is not compiled in. Remove the Pod::PICKER_AUDIO=false statement from your Podfile."
details:nil]);
#endif
} else {
result(FlutterMethodNotImplemented);
_result = nil;
Expand All @@ -127,6 +159,8 @@ - (NSString*)getDocumentDirectory {
}

#pragma mark - Resolvers

#ifdef PICKER_DOCUMENT
- (void)resolvePickDocumentWithMultiPick:(BOOL)allowsMultipleSelection pickDirectory:(BOOL)isDirectory {

@try{
Expand All @@ -150,7 +184,9 @@ - (void)resolvePickDocumentWithMultiPick:(BOOL)allowsMultipleSelection pickDirec

[[self viewControllerWithWindow:nil] presentViewController:self.documentPickerController animated:YES completion:nil];
}
#endif // PICKER_DOCUMENT

#ifdef PICKER_MEDIA
- (void) resolvePickMedia:(MediaType)type withMultiPick:(BOOL)multiPick withCompressionAllowed:(BOOL)allowCompression {

#ifdef PHPicker
Expand Down Expand Up @@ -285,7 +321,9 @@ - (void) resolveMultiPickFromGallery:(MediaType)type withCompressionAllowed:(BOO

[[self viewControllerWithWindow:nil] presentViewController:dkImagePickerController animated:YES completion:nil];
}
#endif // PICKER_MEDIA

#ifdef PICKER_AUDIO
- (void) resolvePickAudioWithMultiPick:(BOOL)isMultiPick {


Expand All @@ -304,6 +342,8 @@ - (void) resolvePickAudioWithMultiPick:(BOOL)isMultiPick {

[[self viewControllerWithWindow:nil] presentViewController:self.audioPickerController animated:YES completion:nil];
}
#endif // PICKER_AUDIO


- (void) handleResult:(id) files {
_result([FileUtils resolveFileInfo: [files isKindOfClass: [NSArray class]] ? files : @[files] withData:self.loadDataToMemory]);
Expand All @@ -312,6 +352,7 @@ - (void) handleResult:(id) files {

#pragma mark - Delegates

#ifdef PICKER_DOCUMENT
// DocumentPicker delegate - iOS 10 only
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url{
[self.documentPickerController dismissViewControllerAnimated:YES completion:nil];
Expand All @@ -336,8 +377,9 @@ - (void)documentPicker:(UIDocumentPickerViewController *)controller

[self handleResult: urls];
}
#endif // PICKER_DOCUMENT


#ifdef PICKER_MEDIA
// ImagePicker delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
if(_result == nil) {
Expand Down Expand Up @@ -503,9 +545,10 @@ -(void)picker:(PHPickerViewController *)picker didFinishPicking:(NSArray<PHPicke
});
}

#endif

#endif // PHPicker
#endif // PICKER_MEDIA

#ifdef PICKER_AUDIO
// AudioPicker delegate
- (void)mediaPicker: (MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
Expand Down Expand Up @@ -536,37 +579,46 @@ - (void)mediaPicker: (MPMediaPickerController *)mediaPicker didPickMediaItems:(M
}
[self handleResult:urls];
}
#endif // PICKER_AUDIO

#pragma mark - Actions canceled

#ifdef PICKER_MEDIA
- (void)presentationControllerDidDismiss:(UIPresentationController *)controller {
Log(@"FilePicker canceled");
if (self.result != nil) {
self.result(nil);
self.result = nil;
}
}
#endif // PICKER_MEDIA

#ifdef PICKER_AUDIO
- (void)mediaPickerDidCancel:(MPMediaPickerController *)controller {
Log(@"FilePicker canceled");
_result(nil);
_result = nil;
[controller dismissViewControllerAnimated:YES completion:NULL];
}
#endif // PICKER_AUDIO

#ifdef PICKER_DOCUMENT
- (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller {
Log(@"FilePicker canceled");
_result(nil);
_result = nil;
[controller dismissViewControllerAnimated:YES completion:NULL];
}
#endif // PICKER_DOCUMENT

#ifdef PICKER_MEDIA
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
Log(@"FilePicker canceled");
_result(nil);
_result = nil;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
#endif

#pragma mark - Alert dialog

Expand Down
41 changes: 28 additions & 13 deletions ios/file_picker.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,35 @@
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
s.name = 'file_picker'
s.version = '0.0.1'
s.summary = 'A new flutter plugin project.'
s.description = <<-DESC
A new flutter plugin project.
s.name = 'file_picker'
s.version = '0.0.1'
s.summary = 'A flutter plugin to show native file picker dialogs'
s.description = <<-DESC
A flutter plugin to show native file picker dialogs.
DESC
s.homepage = 'http://example.com'
s.license = { :file => '../LICENSE' }
s.author = { 'Your Company' => '[email protected]' }
s.source = { :path => '.' }
s.source_files = 'Classes/**/*'
s.public_header_files = 'Classes/**/*.h'
s.dependency 'Flutter'
s.dependency 'DKImagePickerController/PhotoGallery'
s.homepage = 'https://github.com/miguelpruivo/plugins_flutter_file_picker'
s.license = { :file => '../LICENSE' }
s.author = 'Miguel Ruivo'
s.source = { :path => '.' }
s.source_files = 'Classes/**/*'
s.public_header_files = 'Classes/**/*.h'

s.ios.deployment_target = '8.0'

s.dependency 'Flutter'

preprocess_definitions=[]
if !Pod.const_defined?(:PICKER_MEDIA) || PICKER_MEDIA
preprocess_definitions << ["PICKER_MEDIA=1"]
s.dependency 'DKImagePickerController/PhotoGallery'
end
if !Pod.const_defined?(:PICKER_AUDIO) || PICKER_AUDIO
preprocess_definitions << ["PICKER_AUDIO=1"]
end
if !Pod.const_defined?(:PICKER_DOCUMENT) || PICKER_DOCUMENT
preprocess_definitions << ["PICKER_DOCUMENT=1"]
end
s.pod_target_xcconfig = { "GCC_PREPROCESSOR_DEFINITIONS" => preprocess_definitions }

end

2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: A package that allows you to use a native file explorer to pick sin
homepage: https://github.com/miguelpruivo/plugins_flutter_file_picker
repository: https://github.com/miguelpruivo/flutter_file_picker
issue_tracker: https://github.com/miguelpruivo/flutter_file_picker/issues
version: 4.5.1
version: 4.6.0

dependencies:
flutter:
Expand Down

0 comments on commit 5b9dc6e

Please sign in to comment.