Error loading media sources in MLMediaLibrary

I get an error while loading the mediaSources property on Mac OS X.
I am trying to get the source of Apple Photos using the MLMediaLibrary class.
My application is sandboxed and has read-only permission for the image folder.
I get an error message:

MLMediaLibrary error while receiving proxy server of remote object: Domain error = NSCocoaErrorDomain Code = 4097 "connection to the service with the name com.apple.MediaLibraryService" UserInfo = {NSDebugDescription = connection with the service with the name com.apple.MediaLibraryService}

From what I collect, error 4097 is aborted.
I am not very familiar with Swift, but I used the exact same test using the C lens and got the same result.
I assume that I do not have any right.

Here is my (very simplified) code:

 import Foundation import MediaLibrary public class MediaLibrary : NSObject{ var library : MLMediaLibrary! private func loadSources(){ if let mediaSources = library.mediaSources { for (ident, source) in mediaSources{ print("Identifier: \(ident)"); } } } public override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) { loadSources() } public override init(){ super.init() let options : [String : AnyObject] = [MLMediaLoadSourceTypesKey : MLMediaSourceType.Image.rawValue, MLMediaLoadIncludeSourcesKey : MLMediaSourcePhotosIdentifier] library = MLMediaLibrary(options: options) library.addObserver(self, forKeyPath: "mediaSource", options: NSKeyValueObservingOptions.New, context: nil) library.mediaSources; // trigger load, status will be reported back in observeValueForKeyPath } } 
+4
source share
2 answers

Turns out there were two things:
1. MLMediaLoadIncludeSourcesKey should point to an array of strings, not a single row
2. The path of the monitoring key was wrong, it should be "mediaSources" , missing s

+5
source

For reference: under Mojave, the application must be coded in order to work with MLMediaLibrary. A regular code or sandbox worked.

0
source

Source: https://habr.com/ru/post/983491/


All Articles