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

[image_picker] Fix check for iOS 14+ authorization status #6845

Merged
merged 5 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/image_picker/image_picker_ios/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.8.6+4

* Fix authorization status check for iOS14+ so it includes `PHAuthorizationStatusLimited`.

## 0.8.6+3

* Returns error on image load failure.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,4 +397,46 @@ - (void)testSavesImages API_AVAILABLE(ios(14)) {
[self waitForExpectationsWithTimeout:30 handler:nil];
}

- (void)testPickImageRequestAuthorization API_AVAILABLE(ios(14)) {
id mockPhotoLibrary = OCMClassMock([PHPhotoLibrary class]);
OCMStub([mockPhotoLibrary authorizationStatusForAccessLevel:PHAccessLevelReadWrite])
.andReturn(PHAuthorizationStatusNotDetermined);
OCMExpect([mockPhotoLibrary requestAuthorizationForAccessLevel:PHAccessLevelReadWrite
handler:OCMOCK_ANY]);

FLTImagePickerPlugin *plugin = [[FLTImagePickerPlugin alloc] init];

[plugin pickImageWithSource:[FLTSourceSpecification makeWithType:FLTSourceTypeGallery
camera:FLTSourceCameraFront]
maxSize:[[FLTMaxSize alloc] init]
quality:nil
fullMetadata:@YES
completion:^(NSString *result, FlutterError *error){
}];
OCMVerifyAll(mockPhotoLibrary);
}

- (void)testPickImageAuthorizationDenied API_AVAILABLE(ios(14)) {
id mockPhotoLibrary = OCMClassMock([PHPhotoLibrary class]);
OCMStub([mockPhotoLibrary authorizationStatusForAccessLevel:PHAccessLevelReadWrite])
.andReturn(PHAuthorizationStatusDenied);

FLTImagePickerPlugin *plugin = [[FLTImagePickerPlugin alloc] init];

XCTestExpectation *resultExpectation = [self expectationWithDescription:@"result"];

[plugin pickImageWithSource:[FLTSourceSpecification makeWithType:FLTSourceTypeGallery
camera:FLTSourceCameraFront]
maxSize:[[FLTMaxSize alloc] init]
quality:nil
fullMetadata:@YES
completion:^(NSString *result, FlutterError *error) {
XCTAssertNil(result);
XCTAssertEqualObjects(error.code, @"photo_access_denied");
XCTAssertEqualObjects(error.message, @"The user did not allow photo access.");
[resultExpectation fulfill];
}];
[self waitForExpectationsWithTimeout:30 handler:nil];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,13 @@ - (void)checkPhotoAuthorizationWithImagePicker:(UIImagePickerController *)imageP
}

- (void)checkPhotoAuthorizationForAccessLevel API_AVAILABLE(ios(14)) {
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
PHAccessLevel requestedAccessLevel = PHAccessLevelReadWrite;
PHAuthorizationStatus status =
[PHPhotoLibrary authorizationStatusForAccessLevel:requestedAccessLevel];
Comment on lines +368 to +370
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Up to you, but it might be clearer to get rid of the local variable and pass in PHAccessLevelReadWrite in two places. Accessing an enum is so cheap there's no reason to cache it.

Suggested change
PHAccessLevel requestedAccessLevel = PHAccessLevelReadWrite;
PHAuthorizationStatus status =
[PHPhotoLibrary authorizationStatusForAccessLevel:requestedAccessLevel];
PHAuthorizationStatus status =
[PHPhotoLibrary authorizationStatusForAccessLevel:PHAccessLevelReadWrite];

switch (status) {
case PHAuthorizationStatusNotDetermined: {
[PHPhotoLibrary
requestAuthorizationForAccessLevel:PHAccessLevelReadWrite
requestAuthorizationForAccessLevel:requestedAccessLevel
handler:^(PHAuthorizationStatus status) {
dispatch_async(dispatch_get_main_queue(), ^{
if (status == PHAuthorizationStatusAuthorized) {
Expand Down
2 changes: 1 addition & 1 deletion packages/image_picker/image_picker_ios/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: image_picker_ios
description: iOS implementation of the image_picker plugin.
repository: https://github.com/flutter/plugins/tree/main/packages/image_picker/image_picker_ios
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+image_picker%22
version: 0.8.6+3
version: 0.8.6+4

environment:
sdk: ">=2.14.0 <3.0.0"
Expand Down