From c5b68029ce2d13cc05ac6f0f49ecbc1220593bef Mon Sep 17 00:00:00 2001 From: Miguel Ruivo Date: Fri, 30 Nov 2018 18:06:11 +0000 Subject: [PATCH] adds iOS support --- example/ios/Runner.xcodeproj/project.pbxproj | 8 +- .../xcshareddata/xcschemes/Runner.xcscheme | 2 - ios/Classes/FilePickerPlugin.h | 2 +- ios/Classes/FilePickerPlugin.m | 105 +++++++++++------- 4 files changed, 69 insertions(+), 48 deletions(-) diff --git a/example/ios/Runner.xcodeproj/project.pbxproj b/example/ios/Runner.xcodeproj/project.pbxproj index 405f678..4190d80 100644 --- a/example/ios/Runner.xcodeproj/project.pbxproj +++ b/example/ios/Runner.xcodeproj/project.pbxproj @@ -270,7 +270,7 @@ }; AB4C7D1508951531E70F0A36 /* [CP] Embed Pods Frameworks */ = { isa = PBXShellScriptBuildPhase; - buildActionMask = 2147483647; + buildActionMask = 8; files = ( ); inputPaths = ( @@ -281,7 +281,7 @@ outputPaths = ( "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Flutter.framework", ); - runOnlyForDeploymentPostprocessing = 0; + runOnlyForDeploymentPostprocessing = 1; shellPath = /bin/sh; shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh\"\n"; showEnvVarsInLog = 0; @@ -441,7 +441,7 @@ "$(inherited)", "$(PROJECT_DIR)/Flutter", ); - PRODUCT_BUNDLE_IDENTIFIER = com.mr.flutter.plugin.filePickerExamples; + PRODUCT_BUNDLE_IDENTIFIER = com.mr.flutter.plugin.filepickerdemo; PRODUCT_NAME = "$(TARGET_NAME)"; VERSIONING_SYSTEM = "apple-generic"; }; @@ -465,7 +465,7 @@ "$(inherited)", "$(PROJECT_DIR)/Flutter", ); - PRODUCT_BUNDLE_IDENTIFIER = com.mr.flutter.plugin.filePickerExamples; + PRODUCT_BUNDLE_IDENTIFIER = com.mr.flutter.plugin.filepickerdemo; PRODUCT_NAME = "$(TARGET_NAME)"; VERSIONING_SYSTEM = "apple-generic"; }; diff --git a/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme b/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme index 1263ac8..f5a8db1 100644 --- a/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme +++ b/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme @@ -26,7 +26,6 @@ buildConfiguration = "Debug" selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" - language = "" shouldUseLaunchSchemeArgsEnv = "YES"> @@ -46,7 +45,6 @@ buildConfiguration = "Debug" selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" - language = "" launchStyle = "0" useCustomWorkingDirectory = "NO" ignoresPersistentStateOnLaunch = "NO" diff --git a/ios/Classes/FilePickerPlugin.h b/ios/Classes/FilePickerPlugin.h index 4cc8e89..dd57275 100644 --- a/ios/Classes/FilePickerPlugin.h +++ b/ios/Classes/FilePickerPlugin.h @@ -1,4 +1,4 @@ #import -@interface FilePickerPlugin : NSObject +@interface FilePickerPlugin : NSObject @end diff --git a/ios/Classes/FilePickerPlugin.m b/ios/Classes/FilePickerPlugin.m index 20bd6c2..baa99c4 100644 --- a/ios/Classes/FilePickerPlugin.m +++ b/ios/Classes/FilePickerPlugin.m @@ -1,34 +1,58 @@ #import "FilePickerPlugin.h" -@implementation FilePickerPlugin - FlutterResult _result; - UIViewController *_viewController; - UIDocumentPickerViewController *_pickerController; - UIDocumentInteractionController *_interactionController; +@interface FilePickerPlugin() +@property (nonatomic) FlutterResult result; +@property (nonatomic) UIViewController *viewController; +@property (nonatomic) UIDocumentPickerViewController *pickerController; +@property (nonatomic) UIDocumentInteractionController *interactionController; +@property (nonatomic) NSString * fileType; +@end +@implementation FilePickerPlugin + (void)registerWithRegistrar:(NSObject*)registrar { - - FlutterMethodChannel* channel = [FlutterMethodChannel - methodChannelWithName:@"file_picker" - binaryMessenger:[registrar messenger]]; + + FlutterMethodChannel* channel = [FlutterMethodChannel + methodChannelWithName:@"file_picker" + binaryMessenger:[registrar messenger]]; UIViewController *viewController = [UIApplication sharedApplication].delegate.window.rootViewController; FilePickerPlugin* instance = [[FilePickerPlugin alloc] initWithViewController:viewController]; - [registrar addMethodCallDelegate:instance channel:channel]; + [registrar addMethodCallDelegate:instance channel:channel]; } + - (instancetype)initWithViewController:(UIViewController *)viewController { self = [super init]; - if (self) { - _viewController = viewController; - _pickerController = [[UIDocumentPickerViewController alloc] - initWithDocumentTypes:@[@"com.adobe.pdf"] - inMode:UIDocumentPickerModeImport]; + if(self){ + self.viewController = viewController; + } return self; } +- (NSString*) resolveType:(NSString*)type { + + if ([type isEqualToString:@"PDF"]) { + return @"com.adobe.pdf"; + } + else if ([type isEqualToString:@"ANY"]) { + return @"public.item"; + } else { + return nil; + } + +} + +- (void)initPicker { + self.pickerController = [[UIDocumentPickerViewController alloc] + initWithDocumentTypes:@[self.fileType] + inMode:UIDocumentPickerModeImport]; + + self.pickerController.modalPresentationStyle = UIModalPresentationCurrentContext; + self.pickerController.delegate = self; +} + - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { if (_result) { _result([FlutterError errorWithCode:@"multiple_request" @@ -36,37 +60,36 @@ details:nil]); _result = nil; } - - - if ([@"pickPDF" isEqualToString:call.method]) { - - _pickerController.modalPresentationStyle = UIModalPresentationCurrentContext; - _pickerController.delegate = self; - - _result = result; - [_viewController presentViewController:_pickerController animated:YES completion:^{ - if (@available(iOS 11.0, *)) { - _pickerController.allowsMultipleSelection = NO; - } - }]; - - } - else { - result(FlutterMethodNotImplemented); - } + + self.fileType = [self resolveType:call.method]; + + if(self.fileType == nil){ + result(FlutterMethodNotImplemented); + } else { + + [self initPicker]; + _result = result; + [_viewController presentViewController:self.pickerController animated:YES completion:^{ + if (@available(iOS 11.0, *)) { + self.pickerController.allowsMultipleSelection = NO; + } + }]; + + } + } - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray *)urls{ - - [_pickerController dismissViewControllerAnimated:YES completion:nil]; - + + [self.pickerController dismissViewControllerAnimated:YES completion:nil]; + NSString * uri; - + for (NSURL *url in urls) { - uri = (NSString *)[url path]; + uri = (NSString *)[url path]; } - + _result(uri); } @@ -82,12 +105,12 @@ didPickDocumentsAtURLs:(NSArray *)urls{ } - (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application { - + NSLog(@"Starting to send this puppy to %@", application); } - (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application { - + NSLog(@"We're done sending the document."); }