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

Commit 9ee9db6

Browse files
authored
[image_picker] Fix check for iOS 14+ authorization status (#6845)
* fix check for authorization status * add unit tests and update release info * update release info
1 parent 9dc0534 commit 9ee9db6

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

packages/image_picker/image_picker_ios/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.8.6+4
2+
3+
* Fix authorization status check for iOS14+ so it includes `PHAuthorizationStatusLimited`.
4+
15
## 0.8.6+3
26

37
* Returns error on image load failure.

packages/image_picker/image_picker_ios/example/ios/RunnerTests/ImagePickerPluginTests.m

+42
Original file line numberDiff line numberDiff line change
@@ -397,4 +397,46 @@ - (void)testSavesImages API_AVAILABLE(ios(14)) {
397397
[self waitForExpectationsWithTimeout:30 handler:nil];
398398
}
399399

400+
- (void)testPickImageRequestAuthorization API_AVAILABLE(ios(14)) {
401+
id mockPhotoLibrary = OCMClassMock([PHPhotoLibrary class]);
402+
OCMStub([mockPhotoLibrary authorizationStatusForAccessLevel:PHAccessLevelReadWrite])
403+
.andReturn(PHAuthorizationStatusNotDetermined);
404+
OCMExpect([mockPhotoLibrary requestAuthorizationForAccessLevel:PHAccessLevelReadWrite
405+
handler:OCMOCK_ANY]);
406+
407+
FLTImagePickerPlugin *plugin = [[FLTImagePickerPlugin alloc] init];
408+
409+
[plugin pickImageWithSource:[FLTSourceSpecification makeWithType:FLTSourceTypeGallery
410+
camera:FLTSourceCameraFront]
411+
maxSize:[[FLTMaxSize alloc] init]
412+
quality:nil
413+
fullMetadata:@YES
414+
completion:^(NSString *result, FlutterError *error){
415+
}];
416+
OCMVerifyAll(mockPhotoLibrary);
417+
}
418+
419+
- (void)testPickImageAuthorizationDenied API_AVAILABLE(ios(14)) {
420+
id mockPhotoLibrary = OCMClassMock([PHPhotoLibrary class]);
421+
OCMStub([mockPhotoLibrary authorizationStatusForAccessLevel:PHAccessLevelReadWrite])
422+
.andReturn(PHAuthorizationStatusDenied);
423+
424+
FLTImagePickerPlugin *plugin = [[FLTImagePickerPlugin alloc] init];
425+
426+
XCTestExpectation *resultExpectation = [self expectationWithDescription:@"result"];
427+
428+
[plugin pickImageWithSource:[FLTSourceSpecification makeWithType:FLTSourceTypeGallery
429+
camera:FLTSourceCameraFront]
430+
maxSize:[[FLTMaxSize alloc] init]
431+
quality:nil
432+
fullMetadata:@YES
433+
completion:^(NSString *result, FlutterError *error) {
434+
XCTAssertNil(result);
435+
XCTAssertEqualObjects(error.code, @"photo_access_denied");
436+
XCTAssertEqualObjects(error.message, @"The user did not allow photo access.");
437+
[resultExpectation fulfill];
438+
}];
439+
[self waitForExpectationsWithTimeout:30 handler:nil];
440+
}
441+
400442
@end

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,13 @@ - (void)checkPhotoAuthorizationWithImagePicker:(UIImagePickerController *)imageP
365365
}
366366

367367
- (void)checkPhotoAuthorizationForAccessLevel API_AVAILABLE(ios(14)) {
368-
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
368+
PHAccessLevel requestedAccessLevel = PHAccessLevelReadWrite;
369+
PHAuthorizationStatus status =
370+
[PHPhotoLibrary authorizationStatusForAccessLevel:requestedAccessLevel];
369371
switch (status) {
370372
case PHAuthorizationStatusNotDetermined: {
371373
[PHPhotoLibrary
372-
requestAuthorizationForAccessLevel:PHAccessLevelReadWrite
374+
requestAuthorizationForAccessLevel:requestedAccessLevel
373375
handler:^(PHAuthorizationStatus status) {
374376
dispatch_async(dispatch_get_main_queue(), ^{
375377
if (status == PHAuthorizationStatusAuthorized) {

packages/image_picker/image_picker_ios/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: image_picker_ios
22
description: iOS implementation of the image_picker plugin.
33
repository: https://github.com/flutter/plugins/tree/main/packages/image_picker/image_picker_ios
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+image_picker%22
5-
version: 0.8.6+3
5+
version: 0.8.6+4
66

77
environment:
88
sdk: ">=2.14.0 <3.0.0"

0 commit comments

Comments
 (0)