Skip to content

Commit bfc9fa6

Browse files
author
Juan Alvarez
committed
Temporary Fixes until PRs are Merged
Contains code from the following PRs: flutter#2761 flutter#2755
1 parent fe03ef5 commit bfc9fa6

File tree

3 files changed

+18
-21
lines changed

3 files changed

+18
-21
lines changed

packages/image_picker/image_picker/ios/Classes/FLTImagePickerPlugin.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
@interface FLTImagePickerPlugin : NSObject <FlutterPlugin>
88

99
// For testing only.
10-
- (instancetype)initWithViewController:(UIViewController *)viewController;
1110
- (UIImagePickerController *)getImagePickerController;
1211

1312
@end

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,31 @@ @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;
3835
FLTImagePickerPlugin *instance =
39-
[[FLTImagePickerPlugin alloc] initWithViewController:viewController];
36+
[[FLTImagePickerPlugin alloc] init];
4037
[registrar addMethodCallDelegate:instance channel:channel];
4138
}
4239

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

44+
- (UIViewController *)viewController {
45+
UIViewController *topController = UIApplication.sharedApplication.keyWindow.rootViewController;
46+
while (topController.presentedViewController) {
47+
topController = topController.presentedViewController;
48+
}
49+
50+
return topController;
51+
}
52+
5553
- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
5654
if (self.result) {
5755
self.result([FlutterError errorWithCode:@"multiple_request"
@@ -136,7 +134,7 @@ - (void)showCamera {
136134
[UIImagePickerController isCameraDeviceAvailable:_device]) {
137135
_imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
138136
_imagePickerController.cameraDevice = _device;
139-
[_viewController presentViewController:_imagePickerController animated:YES completion:nil];
137+
[[self viewController] presentViewController:_imagePickerController animated:YES completion:nil];
140138
} else {
141139
[[[UIAlertView alloc] initWithTitle:@"Error"
142140
message:@"Camera not available."
@@ -241,7 +239,7 @@ - (void)errorNoPhotoAccess:(PHAuthorizationStatus)status {
241239
- (void)showPhotoLibrary {
242240
// No need to check if SourceType is available. It always is.
243241
_imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
244-
[_viewController presentViewController:_imagePickerController animated:YES completion:nil];
242+
[[self viewController] presentViewController:_imagePickerController animated:YES completion:nil];
245243
}
246244

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

packages/image_picker/image_picker/ios/Tests/ImagePickerPluginTests.m

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ - (void)testPluginPickImageDeviceBack {
2323
return;
2424
}
2525
FLTImagePickerPlugin *plugin =
26-
[[FLTImagePickerPlugin alloc] initWithViewController:[UIViewController new]];
26+
[[FLTImagePickerPlugin alloc] init];
2727
FlutterMethodCall *call =
2828
[FlutterMethodCall methodCallWithMethodName:@"pickImage"
2929
arguments:@{@"source" : @(0), @"cameraDevice" : @(0)}];
@@ -39,7 +39,7 @@ - (void)testPluginPickImageDeviceFront {
3939
return;
4040
}
4141
FLTImagePickerPlugin *plugin =
42-
[[FLTImagePickerPlugin alloc] initWithViewController:[UIViewController new]];
42+
[[FLTImagePickerPlugin alloc] init];
4343
FlutterMethodCall *call =
4444
[FlutterMethodCall methodCallWithMethodName:@"pickImage"
4545
arguments:@{@"source" : @(0), @"cameraDevice" : @(1)}];
@@ -55,7 +55,7 @@ - (void)testPluginPickVideoDeviceBack {
5555
return;
5656
}
5757
FLTImagePickerPlugin *plugin =
58-
[[FLTImagePickerPlugin alloc] initWithViewController:[UIViewController new]];
58+
[[FLTImagePickerPlugin alloc] init];
5959
FlutterMethodCall *call =
6060
[FlutterMethodCall methodCallWithMethodName:@"pickVideo"
6161
arguments:@{@"source" : @(0), @"cameraDevice" : @(0)}];
@@ -71,7 +71,7 @@ - (void)testPluginPickVideoDeviceFront {
7171
return;
7272
}
7373
FLTImagePickerPlugin *plugin =
74-
[[FLTImagePickerPlugin alloc] initWithViewController:[UIViewController new]];
74+
[[FLTImagePickerPlugin alloc] init];
7575
FlutterMethodCall *call =
7676
[FlutterMethodCall methodCallWithMethodName:@"pickVideo"
7777
arguments:@{@"source" : @(0), @"cameraDevice" : @(1)}];
@@ -85,7 +85,7 @@ - (void)testPluginPickVideoDeviceFront {
8585
#pragma mark - Test video duration
8686
- (void)testPickingVideoWithDuration {
8787
FLTImagePickerPlugin *plugin =
88-
[[FLTImagePickerPlugin alloc] initWithViewController:[UIViewController new]];
88+
[[FLTImagePickerPlugin alloc] init];
8989
FlutterMethodCall *call = [FlutterMethodCall
9090
methodCallWithMethodName:@"pickVideo"
9191
arguments:@{@"source" : @(0), @"cameraDevice" : @(0), @"maxDuration" : @95}];
@@ -97,7 +97,7 @@ - (void)testPickingVideoWithDuration {
9797

9898
- (void)testPluginPickImageSelectMultipleTimes {
9999
FLTImagePickerPlugin *plugin =
100-
[[FLTImagePickerPlugin alloc] initWithViewController:[UIViewController new]];
100+
[[FLTImagePickerPlugin alloc] init];
101101
FlutterMethodCall *call =
102102
[FlutterMethodCall methodCallWithMethodName:@"pickImage"
103103
arguments:@{@"source" : @(0), @"cameraDevice" : @(0)}];

0 commit comments

Comments
 (0)