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

Fix photo library authorization status for limited #154

Merged
merged 1 commit into from
Feb 26, 2024
Merged
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
88 changes: 69 additions & 19 deletions Sources/PermissionsSwiftUIPhoto/JMPhotoPermissionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,54 @@ import CorePermissionsSwiftUI

@available(iOS 13.0, tvOS 13.0, *)
public extension PermissionManager {
///Used to access the user's photo library
static let photo = JMPhotoPermissionManager()
///Used to access the user's photo library before iOS 14
@available(iOS, deprecated, obsoleted: 100000, message: "Use 'authorizationStatus(for: )' instead after iOS 14.0.")
static let photo = JMPhotoPermissionManager(requestedAccessLevel: .legacy)

///Used to read-only access the user's photo library
@available(iOS 14.0, *)
static let photoRead = JMPhotoPermissionManager(requestedAccessLevel: .writeOnly)

///Used to read and write to the user's photo library
@available(iOS 14.0, *)
static let photoFull = JMPhotoPermissionManager(requestedAccessLevel: .full)
}

@available(iOS 13.0, tvOS 13.0, *)
public final class JMPhotoPermissionManager: PermissionManager {


public init(requestedAccessLevel: AccessLevel = .legacy) {
self.requestedAccessLevel = requestedAccessLevel
if requestedAccessLevel == .legacy {
NSLog("[PermissionsSwiftUI]: WARNING! Using legacy calendar or reminder permission, which will NOT work in iOS 17 and always return denied due to Apple EventKit API changes. Learn more: https://developer.apple.com/documentation/eventkit/accessing_the_event_store")
}
}
public var requestedAccessLevel: AccessLevel

public enum AccessLevel {
case legacy
@available(iOS 14, *) case writeOnly
@available(iOS 14, *) case full
}

public override var permissionType: PermissionType {
.photo
}

public override var authorizationStatus: AuthorizationStatus {
switch PHPhotoLibrary.authorizationStatus(){
var result: PHAuthorizationStatus? = nil
switch requestedAccessLevel {
case .legacy:
result = PHPhotoLibrary.authorizationStatus()
case .full:
if #available(iOS 14, *) {
result = PHPhotoLibrary.authorizationStatus(for: .readWrite)
}
case .writeOnly:
if #available(iOS 14, *) {
result = PHPhotoLibrary.authorizationStatus(for: .addOnly)
}
}
switch result {
case .authorized:
return .authorized
case .notDetermined:
Expand All @@ -35,22 +69,38 @@ public final class JMPhotoPermissionManager: PermissionManager {
return .denied
}
}

var photoLibrary: PHPhotoLibrary.Type = PHPhotoLibrary.self
init(photoLibrary:PHPhotoLibrary.Type=PHPhotoLibrary.self){
self.photoLibrary = photoLibrary
}

override public func requestPermission(completion: @escaping (Bool, Error?) -> Void) {
photoLibrary.requestAuthorization { authStatus in
switch authStatus {
case .authorized:
completion(true, nil)
case .limited:
completion(true, nil)
default:
completion(false, nil)
switch requestedAccessLevel {
case .legacy:
PHPhotoLibrary.requestAuthorization { status in
self.handleAuthorizationResult(status, completion: completion)
}
case .full:
if #available(iOS 14, *) {
PHPhotoLibrary.requestAuthorization(for: .readWrite) { status in
self.handleAuthorizationResult(status, completion: completion)
}
}
case .writeOnly:
if #available(iOS 14, *) {
PHPhotoLibrary.requestAuthorization(for: .addOnly) { status in
self.handleAuthorizationResult(status, completion: completion)
}
}
default:
completion(false, nil)
}
}

private func handleAuthorizationResult(_ status: PHAuthorizationStatus, completion: @escaping (Bool, Error?) -> Void) {
switch status {
case .authorized, .limited:
completion(true, nil)
default:
completion(false, nil)
}
}

}

Loading