【问题标题】:Error loading media sources in MLMediaLibrary在 MLMediaLibrary 中加载媒体源时出错
【发布时间】:2016-02-29 08:16:40
【问题描述】:

在 Mac OS X 中加载 mediaSources 属性时出错。
我正在尝试使用 MLMediaLibrary 类获取 Apple Photos 源。
我的应用是沙盒化的,并且对图片文件夹具有只读权限。
我收到了错误:

MLMediaLibrary 获取远程对象代理时出错:Error Domain=NSCocoaErrorDomain Code=4097 "connection to service named com.apple.MediaLibraryService" UserInfo={NSDebugDescription=connection to service named com.apple.MediaLibraryService}

据我所知,错误 4097 是连接中断。
我对 Swift 不是很熟悉,但我确实使用 Objective C 运行了完全相同的测试并得到了相同的结果。
我的猜测是我缺少某种权利。

这是我的(非常简化的)代码:

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
    }
}

【问题讨论】:

  • 顺便说一句 - 也尝试过不使用沙盒,结果相同。

标签: swift macos


【解决方案1】:

仅供参考:在 Mojave 下,需要对应用进行代码签名才能使 MLMediaLibrary 正常工作。常规的代码设计或沙盒工作。

【讨论】:

    【解决方案2】:

    原来有两件事不对劲:
    1. MLMediaLoadIncludeSourcesKey 应该指向一个字符串数组,而不是单个字符串
    2. 观察键路径错误,应该是"mediaSources",少了一个s

    【讨论】:

    • 我收到非常相似的错误,但在 AVPlayer 尝试从 URL 站点加载视频时:请参阅 SO -“AVPlayer 不允许我访问 URL”startConfigurationWithCompletionHandler:无法获取远程对象代理:错误域=NSCocoaErrorDomain 代码=4097 “连接到名为 com.apple.rtcreportingd 的服务” UserInfo={NSDebugDescription=连接到名为 com.apple.rtcreportingd 的服务} 2017-11-10 19:48:50.095255-0800 AVPlayer[3784:207465] initWithSessionInfo : XPC 连接中断 - 知道这是什么吗?
    猜你喜欢
    • 2014-07-24
    • 1970-01-01
    • 2015-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多