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

Conversation

vashworth
Copy link
Contributor

@vashworth vashworth commented Dec 14, 2022

Replace [PHPhotoLibrary authorizationStatus] with [PHPhotoLibrary authorizationStatusForAccessLevel:] for iOS 14+.

According to WWDC 2020 video "Handle the Limited Photos Library in your app", authorizationStatus is deprecated and will not return the newer PHAuthorizationStatusLimited.

Fixes flutter/flutter#113681.

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the relevant style guides and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/plugins repo does use dart format.)
  • I signed the CLA.
  • The title of the PR starts with the name of the plugin surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.
  • I updated CHANGELOG.md to add a description of the change, following repository CHANGELOG style.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@vashworth
Copy link
Contributor Author

So I'm unsure of how to test this change. Since the behavior and UI for PHAuthorizationStatusLimited and PHAuthorizationStatusAuthorized is the same, I can't think of a way to test it with a UITest. And since we're dealing with permissions, I don't think a normal unit test would work either.

Also, I'm waiting to update the changelog/pubspec since I have a different PR for the image_picker that should land soon.

@vashworth vashworth marked this pull request as ready for review December 15, 2022 00:17
@vashworth vashworth requested a review from cyanglaz as a code owner December 15, 2022 00:17
@vashworth vashworth requested a review from jmagman December 15, 2022 00:17
@jmagman
Copy link
Member

jmagman commented Dec 15, 2022

So I'm unsure of how to test this change. Since the behavior and UI for PHAuthorizationStatusLimited and PHAuthorizationStatusAuthorized is the same, I can't think of a way to test it with a UITest. And since we're dealing with permissions, I don't think a normal unit test would work either.

Ugh, sorry, the checkPhotoAuthorizationForAccessLevel path is totally untested, and the test file is kind of a mess. This would work:

- (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];
}

The first one would fail via OCMExpect/OCMVerifyAll if -requestAuthorizationForAccessLevel:PHAccessLevelReadWrite wasn't called. The second would fail if the completion didn't fail with the photo_access_denied error.

Copy link
Member

@jmagman jmagman left a comment

Choose a reason for hiding this comment

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

LGTM!

Comment on lines +371 to +373
PHAccessLevel requestedAccessLevel = PHAccessLevelReadWrite;
PHAuthorizationStatus status =
[PHPhotoLibrary authorizationStatusForAccessLevel:requestedAccessLevel];
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];

Copy link
Contributor

@cyanglaz cyanglaz left a comment

Choose a reason for hiding this comment

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

LGTM!

@vashworth vashworth added the autosubmit Merge PR when tree becomes green via auto submit App label Dec 22, 2022
@auto-submit auto-submit bot merged commit 9ee9db6 into flutter:main Dec 23, 2022
auto-submit bot pushed a commit to flutter/flutter that referenced this pull request Dec 26, 2022
* 986b16a2d Roll Flutter from 9fb1ae8 to a45a2f3 (19 revisions) (flutter/plugins#6879)

* 9dc053428 expose webresourceeerrortype (flutter/plugins#6877)

* 9ee9db676 [image_picker] Fix check for iOS 14+ authorization status (flutter/plugins#6845)

* 3eba2bf69 Roll Flutter from a45a2f3 to e766ad0 (7 revisions) (flutter/plugins#6880)
loic-sharma pushed a commit to fluttergithubbot/flutter that referenced this pull request Jan 6, 2023
…#117653)

* 986b16a2d Roll Flutter from 9fb1ae8 to a45a2f3 (19 revisions) (flutter/plugins#6879)

* 9dc053428 expose webresourceeerrortype (flutter/plugins#6877)

* 9ee9db676 [image_picker] Fix check for iOS 14+ authorization status (flutter/plugins#6845)

* 3eba2bf69 Roll Flutter from a45a2f3 to e766ad0 (7 revisions) (flutter/plugins#6880)
gspencergoog pushed a commit to gspencergoog/flutter that referenced this pull request Jan 19, 2023
…#117653)

* 986b16a2d Roll Flutter from 9fb1ae8 to a45a2f3 (19 revisions) (flutter/plugins#6879)

* 9dc053428 expose webresourceeerrortype (flutter/plugins#6877)

* 9ee9db676 [image_picker] Fix check for iOS 14+ authorization status (flutter/plugins#6845)

* 3eba2bf69 Roll Flutter from a45a2f3 to e766ad0 (7 revisions) (flutter/plugins#6880)
mauricioluz pushed a commit to mauricioluz/plugins that referenced this pull request Jan 26, 2023
* fix check for authorization status

* add unit tests and update release info

* update release info
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
autosubmit Merge PR when tree becomes green via auto submit App p: image_picker platform-ios
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[image_picker] [PHPhotoLibrary authorizationStatus] status is not correct in iOS14+
3 participants