Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 9f58745

Browse files
chris-rutkowskichris-agoda
authored andcommitted
Makes dynamic lookup for the top view controller in order to present image or camera picker.
Existing implementation is incorrect especially when adding project as a module with more complex presentation structure. If window's root view controller has presented modally any other view controller, it cannot present any other any more. Only the top most controller can present other. With this change, the image picker/camera controller is presented on the top most view controller.
1 parent 486da06 commit 9f58745

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

AUTHORS

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,5 @@ Giancarlo Rocha <giancarloiff@gmail.com>
5656
Ryo Miyake <ryo@miyake.id>
5757
Théo Champion <contact.theochampion@gmail.com>
5858
Kazuki Yamaguchi <y.kazuki0614n@gmail.com>
59-
Eitan Schwartz <eshvartz@gmail.com>
59+
Eitan Schwartz <eshvartz@gmail.com>
60+
Chris Rutkowski <chrisrutkowski89@gmail.com>

packages/image_picker/image_picker/ios/Classes/FLTImagePickerPlugin.m

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,29 @@ @interface FLTImagePickerPlugin () <UINavigationControllerDelegate, UIImagePicke
2525
@implementation FLTImagePickerPlugin {
2626
NSDictionary *_arguments;
2727
UIImagePickerController *_imagePickerController;
28-
UIViewController *_viewController;
2928
UIImagePickerControllerCameraDevice _device;
3029
}
3130

3231
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
3332
FlutterMethodChannel *channel =
3433
[FlutterMethodChannel methodChannelWithName:@"plugins.flutter.io/image_picker"
3534
binaryMessenger:[registrar messenger]];
36-
UIViewController *viewController =
37-
[UIApplication sharedApplication].delegate.window.rootViewController;
38-
FLTImagePickerPlugin *instance =
39-
[[FLTImagePickerPlugin alloc] initWithViewController:viewController];
35+
FLTImagePickerPlugin *instance = [FLTImagePickerPlugin new];
4036
[registrar addMethodCallDelegate:instance channel:channel];
4137
}
4238

43-
- (instancetype)initWithViewController:(UIViewController *)viewController {
44-
self = [super init];
45-
if (self) {
46-
_viewController = viewController;
47-
}
48-
return self;
49-
}
50-
5139
- (UIImagePickerController *)getImagePickerController {
5240
return _imagePickerController;
5341
}
5442

43+
- (UIViewController *)viewController {
44+
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
45+
while (topController.presentedViewController) {
46+
topController = topController.presentedViewController;
47+
}
48+
return topController;
49+
}
50+
5551
- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
5652
if (self.result) {
5753
self.result([FlutterError errorWithCode:@"multiple_request"
@@ -136,7 +132,7 @@ - (void)showCamera {
136132
[UIImagePickerController isCameraDeviceAvailable:_device]) {
137133
_imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
138134
_imagePickerController.cameraDevice = _device;
139-
[_viewController presentViewController:_imagePickerController animated:YES completion:nil];
135+
[[self viewController] presentViewController:_imagePickerController animated:YES completion:nil];
140136
} else {
141137
[[[UIAlertView alloc] initWithTitle:@"Error"
142138
message:@"Camera not available."
@@ -241,7 +237,7 @@ - (void)errorNoPhotoAccess:(PHAuthorizationStatus)status {
241237
- (void)showPhotoLibrary {
242238
// No need to check if SourceType is available. It always is.
243239
_imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
244-
[_viewController presentViewController:_imagePickerController animated:YES completion:nil];
240+
[[self viewController] presentViewController:_imagePickerController animated:YES completion:nil];
245241
}
246242

247243
- (void)imagePickerController:(UIImagePickerController *)picker

0 commit comments

Comments
 (0)