0

I'm trying to access photos stored in macOS Photos app and have this code in ViewController class.

import Cocoa
import Photos

class vcSecond: NSViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    let fetchOptions = PHFetchOptions()
    fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
    let identifiers = PHPhotoLibrary.localIdentifiers(PHPhotoLibrary())
    let assets = PHAsset.fetchAssets(withLocalIdentifiers: identifiers, options: fetchOptions)
    print(assets.count)
}
}

and I'm struggled with getting of localIdentifiers for fetchAssets method. XCode gives me error "Cannot convert value of type '([PHCloudIdentifier]) -> [String]' to expected argument type '[String]'". There are many code samples availbale for iOS but much lees for macOS. Is anyone willing to share some codes and/or provide any hints? Thanks.

Dawy
  • 770
  • 6
  • 23
  • Possible duplicate of [How to get all PHAssets from MacOS's Photo Library](https://stackoverflow.com/questions/47652944/how-to-get-all-phassets-from-macoss-photo-library) – Arie Pinto Dec 27 '18 at 14:46

1 Answers1

1

Well if all you are trying to do is access the photos then to open the Photo app just add this code snippet to your controller

override func viewDidLoad() {
    super.viewDidLoad()

    NSWorkspace.shared.open(URL(fileURLWithPath: "/Applications/Photos.app"))
}

Also, see this thread: How to get all PHAssets from MacOS's Photo Library

Arie Pinto
  • 1,274
  • 1
  • 11
  • 19
  • Thanks. Your suggestion to use NSWorkspace works fine but my original aim was to open slected photo/s in my own app. If I understood well it's possible only via Photos app extension, right? Or I could possibly store photos in file folder and then access it from the app. But I`d like to skip this extra step. – Dawy Dec 27 '18 at 16:45
  • Not sure, but from what I understood from that link I attached it's not possible. – Arie Pinto Dec 27 '18 at 16:47