adds support for video files

This commit is contained in:
Miguel Ruivo 2018-12-05 15:32:02 +00:00
parent cf8ceb57da
commit 2826379e15
7 changed files with 109 additions and 62 deletions

View File

@ -136,6 +136,8 @@ public class FilePickerPlugin implements MethodCallHandler {
switch (type){
case "PDF":
return "application/pdf";
case "VIDEO":
return "video/*";
case "ANY":
return "*/*";
default:

View File

@ -29,12 +29,6 @@ class _MyAppState extends State<MyApp> {
});
}
// Platform messages are asynchronous, so we initialize in an async method.
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
@override
Widget build(BuildContext context) {
return new MaterialApp(
@ -64,15 +58,21 @@ class _MyAppState extends State<MyApp> {
child: new Text('FROM PDF'),
value: FileType.PDF,
),
new DropdownMenuItem(
child: new Text('VIDEO'),
value: FileType.VIDEO,
),
new DropdownMenuItem(
child: new Text('ANY'),
value: FileType.ANY,
)
],
onChanged: (value) {
setState(() {
_pickingType = value;
});
setState(
() {
_pickingType = value;
},
);
},
),
),

View File

@ -1,4 +1,6 @@
#import <Flutter/Flutter.h>
#import <UIKit/UIKit.h>
#import <MobileCoreServices/MobileCoreServices.h>
@interface FilePickerPlugin : NSObject<FlutterPlugin, UIDocumentPickerDelegate>
@interface FilePickerPlugin : NSObject<FlutterPlugin, UIDocumentPickerDelegate, UITabBarDelegate, UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@end

View File

@ -1,4 +1,5 @@
#import "FilePickerPlugin.h"
#import "FileUtils.h"
@interface FilePickerPlugin()
@property (nonatomic) FlutterResult result;
@ -24,30 +25,18 @@
- (instancetype)initWithViewController:(UIViewController *)viewController {
self = [super init];
if(self){
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];
initWithDocumentTypes:@[self.fileType]
inMode:UIDocumentPickerModeImport];
self.pickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
self.pickerController.delegate = self;
@ -61,20 +50,26 @@
_result = nil;
}
self.fileType = [self resolveType:call.method];
_result = result;
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;
}
}];
if([call.method isEqualToString:@"VIDEO"]) {
[self resolvePickVideo];
}
else {
self.fileType = [FileUtils resolveType:call.method];
if(self.fileType == nil){
result(FlutterMethodNotImplemented);
} else {
[self initPicker];
[_viewController presentViewController:self.pickerController animated:YES completion:^{
if (@available(iOS 11.0, *)) {
self.pickerController.allowsMultipleSelection = NO;
}
}];
}
}
}
@ -83,35 +78,32 @@
didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls{
[self.pickerController dismissViewControllerAnimated:YES completion:nil];
NSString * uri;
for (NSURL *url in urls) {
uri = (NSString *)[url path];
}
_result(uri);
_result([FileUtils resolvePath:urls]);
}
// DocumentInteractionController delegate
- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller {
_result(@"Finished");
}
// VideoPicker delegate
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
NSLog(@"Finished");
return _viewController;
}
- (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application {
- (void) resolvePickVideo{
NSLog(@"Starting to send this puppy to %@", application);
UIImagePickerController *videoPicker = [[UIImagePickerController alloc] init];
videoPicker.delegate = self;
videoPicker.modalPresentationStyle = UIModalPresentationCurrentContext;
videoPicker.mediaTypes = @[(NSString*)kUTTypeMovie, (NSString*)kUTTypeAVIMovie, (NSString*)kUTTypeVideo, (NSString*)kUTTypeMPEG4];
videoPicker.videoQuality = UIImagePickerControllerQualityTypeHigh;
[self.viewController presentViewController:videoPicker animated:YES completion:nil];
}
- (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application {
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(@"We're done sending the document.");
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
[picker dismissViewControllerAnimated:YES completion:NULL];
_result([videoURL path]);
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
@end

13
ios/Classes/FileUtils.h Normal file
View File

@ -0,0 +1,13 @@
//
// FileUtils.h
// Pods
//
// Created by Miguel Ruivo on 05/12/2018.
//
@interface FileUtils : NSObject
+ (NSString*) resolveType:(NSString*)type;
+ (NSString*) resolvePath:(NSArray<NSURL *> *)urls;
@end

35
ios/Classes/FileUtils.m Normal file
View File

@ -0,0 +1,35 @@
//
// FileUtils.m
// file_picker
//
// Created by Miguel Ruivo on 05/12/2018.
//
#import "FileUtils.h"
@implementation FileUtils
+ (NSString*) resolveType:(NSString*)type {
if ([type isEqualToString:@"PDF"]) {
return @"com.adobe.pdf";
}
else if ([type isEqualToString:@"ANY"]) {
return @"public.item";
} else {
return nil;
}
}
+ (NSString*) resolvePath:(NSArray<NSURL *> *)urls{
NSString * uri;
for (NSURL *url in urls) {
uri = (NSString *)[url path];
}
return uri;
}
@end

View File

@ -7,6 +7,7 @@ enum FileType {
ANY,
PDF,
IMAGE,
VIDEO,
CAPTURE,
}
@ -23,12 +24,14 @@ class FilePicker {
static Future<String> getFilePath({FileType type = FileType.ANY}) async {
switch (type) {
case FileType.PDF:
return _getPath('PDF');
case FileType.IMAGE:
return _getImage(ImageSource.gallery);
case FileType.CAPTURE:
return _getImage(ImageSource.camera);
case FileType.PDF:
return _getPath('PDF');
case FileType.VIDEO:
return _getPath('VIDEO');
default:
return _getPath('ANY');
}