Skip to content

Commit

Permalink
Fixes iOS ViewController which is nil when UIWindow.rootViewControlle…
Browse files Browse the repository at this point in the history
…r have changed (#525)
  • Loading branch information
Miguel Ruivo committed Dec 13, 2020
1 parent 692d798 commit e2feb9b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
## 2.1.4
iOS: Fixes iOS ViewController which is nil when UIWindow.rootViewController have changed. ([#525](https://github.com/miguelpruivo/flutter_file_picker/issues/525)). Thank you @devcxm.

## 2.1.3
Android: Updates file name handling method. ([#487](https://github.com/miguelpruivo/flutter_file_picker/issues/487)).
Desktop (Go): Fixed desktop plugin implementation (thank you @DenchikBY).

## 2.1.2
Desktop (Go): Fixed desktop plugin implementation. Thank you @DenchikBY.

## 2.1.1
iOS: Fixes an issue that could result in a crash when selecting a media item twice. ([#518](https://github.com/miguelpruivo/flutter_file_picker/issues/518)).
Expand Down
27 changes: 16 additions & 11 deletions ios/Classes/FilePickerPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@interface FilePickerPlugin() <DKImageAssetExporterObserver>
@property (nonatomic) FlutterResult result;
@property (nonatomic) FlutterEventSink eventSink;
@property (nonatomic) UIViewController *viewController;
@property (nonatomic, readonly) UIViewController *viewController;
@property (nonatomic) UIImagePickerController *galleryPickerController;
@property (nonatomic) UIDocumentPickerViewController *documentPickerController;
@property (nonatomic) UIDocumentInteractionController *interactionController;
Expand All @@ -28,22 +28,26 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
eventChannelWithName:@"miguelruivo.flutter.plugins.filepickerevent"
binaryMessenger:[registrar messenger]];

UIViewController *viewController = [UIApplication sharedApplication].delegate.window.rootViewController;
FilePickerPlugin* instance = [[FilePickerPlugin alloc] initWithViewController:viewController];
FilePickerPlugin* instance = [[FilePickerPlugin alloc] init];

[registrar addMethodCallDelegate:instance channel:channel];
[eventChannel setStreamHandler:instance];
}

- (instancetype)initWithViewController:(UIViewController *)viewController {
- (instancetype)init {
self = [super init];
if(self) {
self.viewController = viewController;
}

return self;
}

- (UIViewController *)viewController {
UIViewController *rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
while (rootViewController.presentedViewController) {
rootViewController = rootViewController.presentedViewController;
}
return rootViewController;
}

- (FlutterError *)onListenWithArguments:(id)arguments eventSink:(FlutterEventSink)events {
_eventSink = events;
return nil;
Expand Down Expand Up @@ -133,7 +137,7 @@ - (void)resolvePickDocumentWithMultiPick:(BOOL)allowsMultipleSelection pickDirec
self.documentPickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
self.galleryPickerController.allowsEditing = NO;

[_viewController presentViewController:self.documentPickerController animated:YES completion:nil];
[self.viewController presentViewController:self.documentPickerController animated:YES completion:nil];
}

- (void) resolvePickMedia:(MediaType)type withMultiPick:(BOOL)multiPick withCompressionAllowed:(BOOL)allowCompression {
Expand Down Expand Up @@ -199,9 +203,10 @@ - (void) resolveMultiPickFromGallery:(MediaType)type withCompressionAllowed:(BOO
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleAlert];
UIActivityIndicatorView* indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];

UIViewController *currentViewController = self.viewController;
if(_eventSink == nil) {
// Create alert dialog for asset caching
[alert.view setCenter: _viewController.view.center];
[alert.view setCenter: currentViewController.view.center];
[alert.view addConstraint: [NSLayoutConstraint constraintWithItem:alert.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:100]];

// Create a default loader if user don't provide a status handler
Expand Down Expand Up @@ -233,7 +238,7 @@ - (void) resolveMultiPickFromGallery:(MediaType)type withCompressionAllowed:(BOO
self->_eventSink([NSNumber numberWithBool:YES]);
} else {
[indicator startAnimating];
[self->_viewController showViewController:alert sender:nil];
[currentViewController showViewController:alert sender:nil];
}

} else {
Expand Down Expand Up @@ -264,7 +269,7 @@ - (void) resolveMultiPickFromGallery:(MediaType)type withCompressionAllowed:(BOO
[self handleResult: paths];
}];

[_viewController presentViewController:dkImagePickerController animated:YES completion:nil];
[self.viewController presentViewController:dkImagePickerController animated:YES completion:nil];
}

- (void) resolvePickAudioWithMultiPick:(BOOL)isMultiPick {
Expand Down

0 comments on commit e2feb9b

Please sign in to comment.