2015-11-26 13 views
4

在Mac OS X中加載mediaSources屬性時出現錯誤。
我試圖使用MLMediaLibrary類獲取Apple Photos源。
我的應用程序是沙盒,並具有圖片文件夾的只讀權限。
我發現了錯誤:在MLMediaLibrary中加載媒體源時出錯

MLMediaLibrary error obtaining remote object proxy: Error Domain=NSCocoaErrorDomain Code=4097 "connection to service named com.apple.MediaLibraryService" UserInfo={NSDebugDescription=connection to service named com.apple.MediaLibraryService}

從我所收集,錯誤4097中斷連接。
我對Swift並不是很熟悉,但是我使用目標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 
    } 
} 
+0

順便說一句 - 也嘗試沒有沙箱,結果相同。 –

回答

5

原來有不對勁兩件事情:
1. MLMediaLoadIncludeSourcesKey應指向字符串數組,而不是一個字符串
2.觀察關鍵路徑是錯誤的,應該是"mediaSources",錯過了s

+0

我收到了非常類似的錯誤,但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連接中斷 - 任何想法是什麼? –