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

Commit

Permalink
[image_picker] Fix check for iOS 14+ authorization status (#6845)
Browse files Browse the repository at this point in the history
* fix check for authorization status

* add unit tests and update release info

* update release info
  • Loading branch information
vashworth committed Dec 23, 2022
1 parent 9dc0534 commit 9ee9db6
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
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];
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

0 comments on commit 9ee9db6

Please sign in to comment.