Skip to content

Commit

Permalink
Handle Photo Library privacy settings correctly
Browse files Browse the repository at this point in the history
If photo library access is denied by the user then FreeOTP will no longer attempt to
read from the photo gallery, falling back to the default FreeOTP token icon.

Resolves: #258
  • Loading branch information
justin-stephenson committed Jan 17, 2022
1 parent 757779e commit 1cdd17f
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions FreeOTP/ImageDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ class ImageDownloader : NSObject {
super.init()
}

func isPHAssetAuthorized(_ status: PHAuthorizationStatus) -> Bool! {
switch status {
case .denied, .restricted:
return false
case .notDetermined:
return nil
case .authorized, .limited:
return true
@unknown default:
return false
}
}

func fromPHAsset(_ asset: PHAsset, completion: @escaping (UIImage) -> Void) {
let opts: PHImageRequestOptions = PHImageRequestOptions()
opts.isSynchronous = true
Expand All @@ -49,13 +62,28 @@ class ImageDownloader : NSObject {
}

func fromALAsset(_ asset: URL, completion: @escaping (UIImage) -> Void) {
if asset.scheme == "assets-library" {
let rslt = PHAsset.fetchAssets(withALAssetURLs: [asset], options: nil)
if rslt.count > 0 {
return fromPHAsset(rslt[0] , completion: completion)
let status = PHPhotoLibrary.authorizationStatus()
let authorized = isPHAssetAuthorized(status)
var access_granted = false

if (authorized == false) {
// shortcut completion
} else if (authorized == nil) {
PHPhotoLibrary.requestAuthorization { (status) -> Void in
if (self.isPHAssetAuthorized(status) == true) {
access_granted = true
}
}
}

if (authorized == true || access_granted == true) {
if asset.scheme == "assets-library" {
let rslt = PHAsset.fetchAssets(withALAssetURLs: [asset], options: nil)
if rslt.count > 0 {
return fromPHAsset(rslt[0] , completion: completion)
}
}
}
return completion(DEFAULT)
}

Expand Down

0 comments on commit 1cdd17f

Please sign in to comment.