Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept limited photo library authorization #22

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 4 additions & 5 deletions Queryable/Queryable/PhotoHelper/PhotoLibrary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@ import Photos
import os.log

class PhotoLibrary {

static func checkAuthorization() async -> Bool {
switch PHPhotoLibrary.authorizationStatus(for: .readWrite) {
static func checkAuthorization(status: PHAuthorizationStatus = PHPhotoLibrary.authorizationStatus(for: .readWrite)) async -> Bool {
switch status {
Copy link
Author

Choose a reason for hiding this comment

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

whoops almost forgot to swap this out

I think I don't like using the default argument in this way, but waiting for feedback re: logs & style before changing anything

case .authorized:
logger.debug("Photo library access authorized.")
return true
case .notDetermined:
logger.debug("Photo library access not determined.")
return await PHPhotoLibrary.requestAuthorization(for: .readWrite) == .authorized
return await checkAuthorization(status: PHPhotoLibrary.requestAuthorization(for: .readWrite))
Copy link
Author

Choose a reason for hiding this comment

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

Okay, I think this fixes the issue! I was only considering limited authorization when checking if already authorized. I also needed to accept limited authorization at the time of request.

Let me know if you want me to restructure this code somehow. I think the recursive nature is a bit weird, but I assume the OS won't return notDetermined repeatedly. (Maybe they'll add a cancel button in the future that changes that though?)

It's worth noting there's a minor logging behavior change here. Previously, you wouldn't log the new status after requesting authorization. Now, we do.

If you want me to refactor this, let me know how! If logging new status after request isn't your preference, I can just do something simple like [.authorized, .limited].contains(PHPhotoLibrary.requestAuthorization(for: .readWrite)).

case .denied:
logger.debug("Photo library access denied.")
return false
case .limited:
logger.debug("Photo library access limited.")
return false
return true
case .restricted:
logger.debug("Photo library access restricted.")
return false
Expand Down