Skip to content

Commit

Permalink
Use iOS 14 photo permission api (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
jevonmao authored Feb 26, 2024
1 parent 94b6eaf commit da581a2
Showing 1 changed file with 69 additions and 19 deletions.
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)
}
}

}

0 comments on commit da581a2

Please sign in to comment.