52

I want to get all the photos of my custom album. but instead what I get is the below error.

My Code

let collections:PHFetchResult = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)

Error i get

"Error returned from daemon: Error Domain=com.apple.accounts Code=7 "(null)""

Any ideas on how to fix this?

chirag90
  • 2,211
  • 1
  • 22
  • 37
Björn Lindner
  • 521
  • 1
  • 4
  • 4
  • 1
    add first `Privacy - Photo Library Usage Descriptionkey` in your info.plist and then try to run this code – Divyesh Gondaliya Sep 05 '19 at 10:10
  • that was my first thought too, but I already set that. `let authState = PHPhotoLibrary.authorizationStatus()` -> authorized – Björn Lindner Sep 05 '19 at 10:39
  • `swift` and `Xcode` version? – TheTiger Sep 05 '19 at 10:55
  • 2
    Xcode Version 11.0 beta 6 (11M392r) Swift 5 – Björn Lindner Sep 05 '19 at 14:12
  • @BjörnLindner Could you confirm if it happens on non beta version of Xcode like Xcode 10.2? – TheTiger Sep 06 '19 at 05:22
  • I can confirm this on Xcode 11, running on a device that has iCloud photos enabled. Thinking that's the culprit, but I don't know how to fix it yet. – promacuser Sep 26 '19 at 13:33
  • Hey @chirag90, did you manage to find a fix? – marcelosalloum Oct 08 '19 at 21:41
  • 1
    I'm getting this error printed in the terminal but the `PHAssets` are being correctly retrieved. When I try to call the `PHImageManager().requestImage` in Xcode11 though, I need to specify `options.deliveryMode = .highQualityFormat`, while it used to work just fine with `.fastFormat` back in Xcode10. – marcelosalloum Oct 09 '19 at 12:45
  • @BjörnLindner By any chance were you able to figure this out? I'm having the same issue. – albertski Nov 13 '19 at 13:48
  • I am having a similar problem when I am calling for all the photos as well. I thought it might be because of the large number of images in my library and reduced it using the fetchlimit parameter. Unfortunately didn't make a difference. I am looking for a solution. Has anyone solved this yet? – Taylor Maxwell Feb 06 '20 at 18:12
  • I _can_ get the photos from the library (videos, in my case) but I still get this "Error" (effectively, a _warning_) on the console... – Nicolas Miari Feb 12 '20 at 07:14
  • I can get the photos from the library but I still get this "Error" (effectively, a warning) on the console... +1 – frank61003 Apr 14 '20 at 06:37
  • 1
    I have the same error on Xcode version 11.5 – iAleksandr May 30 '20 at 17:11
  • Has someone managed to solve this issue? please share... – sharon Jun 07 '20 at 20:39
  • 4
    This doesn't actually stop you from fetching the assets. It could be a bug on Apple's side - file it on Feedback Assistant.app. – Pranav Kasetti Aug 02 '20 at 10:07

3 Answers3

1

Based on the comments, I'm not entirely sure what the issue is but I hope this code could provide some assistance. Using .album rather than .smartAlbum could also be part of the issue.

private var fetchResult: PHFetchResult<PHAsset>!

func fetchOptions(_ predicate: NSPredicate?) -> PHFetchOptions {
            let options = PHFetchOptions()
            options.sortDescriptors = [ NSSortDescriptor(key: "creationDate", ascending: false) ]
        options.predicate = predicate
        return options
    }
}

    if let userLibraryCollection = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .smartAlbumUserLibrary, options: nil).firstObject {
        self.fetchResult = PHAsset.fetchAssets(in: userLibraryCollection, options: fetchOptions(NSPredicate(format: "mediaType = \(PHAssetMediaType.image.rawValue)")))
    } else {
        self.fetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions(nil))
    }
0
private var fetchResult: PHFetchResult<PHAsset>!
    
func picker(_ picker: PHPickerViewController, didFinishPicking 
 results: [PHPickerResult]) {
    self.dismiss(animated: true, completion: nil)
    
    
    if let userLibraryCollection = 
 PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: 
 .smartAlbumUserLibrary, options: nil).firstObject {
        
        self.fetchResult = PHAsset.fetchAssets(in: 
     userLibraryCollection, options: fetchOptions(NSPredicate(format: 
     "mediaType = \(PHAssetMediaType.image.rawValue)")))
        
        fetchResult.enumerateObjects { asset, index, stop in
            PHAsset.getURL(ofPhotoWith: asset) { (url) in
                
                if let imgurl = url{
                    print(imgurl)
                }else {
                    print("error")
                }
                
            }
            
        }
        
        
        
    } else {
        self.fetchResult = PHAsset.fetchAssets(with: .image, options: 
     fetchOptions(nil))
        
        print(fetchResult.firstObject)
    }
}



  extension PHAsset {
static func getURL(ofPhotoWith mPhasset: PHAsset, completionHandler : @escaping ((_ responseURL : URL?) -> Void)) {
    if mPhasset.mediaType == .image {
        let options: PHContentEditingInputRequestOptions = PHContentEditingInputRequestOptions()
        options.canHandleAdjustmentData = {(adjustmeta: PHAdjustmentData) -> Bool in
            return true
        }
        mPhasset.requestContentEditingInput(with: options, completionHandler: { (contentEditingInput, info) in
            if let fullSizeImageUrl = contentEditingInput?.fullSizeImageURL {
                completionHandler(fullSizeImageUrl)
            } else {
                completionHandler(nil)
            }
        })
    } else if mPhasset.mediaType == .video {
        let options: PHVideoRequestOptions = PHVideoRequestOptions()
        options.version = .original
        PHImageManager.default().requestAVAsset(forVideo: mPhasset, options: options, resultHandler: { (asset, audioMix, info) in
            if let urlAsset = asset as? AVURLAsset {
                let localVideoUrl = urlAsset.url
                completionHandler(localVideoUrl)
            } else {
                completionHandler(nil)
            }
        })
       }
    
    }
 }
Sahil Omer
  • 163
  • 9
0

Do everything with PHAsset on the main thread. I still get that error, but at least images are fetched.

let options = PHFetchOptions()
options.includeHiddenAssets = false
options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)] // Newest first
options.predicate = NSPredicate(format: "mediaType = \(PHAssetMediaType.image.rawValue)") // Only images

DispatchQueue.main.async {
    self.mediaAssets = PHAsset.fetchAssets(with: options)
}
Denis Kutlubaev
  • 15,320
  • 6
  • 84
  • 70