Hello,
I'm developing an application which is basically downloading and saving subtitles for video files.
A user select a video file (either with an NSOpenPanel or drag an dropping it into the app) and I download and save subtitle file in the same folder of the video file using the same file name but with "srt" as extension (example: if the video file name is "movie_1.mp4" the subtitle file will be "movie_1.srt").
According to the sandbox specs such a behavior should be allowed.
Here what I did:
- Enabled the sandboxing for the app
- enabled com.apple.security.network.client entitlements (which is working fine)
- enabled com.apple.security.files.user-selected.read-write
- Added the following configuration to my Info.plist: (adding both mp4 and srt doc types, and fligging srt as related item)
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeExtensions</key>
<array>
<string>mp4</string>
<string>mpeg4</string>
<string>m4v</string>
</array>
<key>CFBundleTypeName</key>
<string>MPEG-4 File</string>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
</dict>
<dict>
<key>CFBundleTypeExtensions</key>
<array>
<string>srt</string>
</array>
<key>CFBundleTypeName</key>
<string>SRT File</string>
<key>CFBundleTypeRole</key>
<string>None</string>
<key>NSIsRelatedItemType</key>
<true/>
</dict>
</array>
Then I created a class that implement the NSFilePresenter protocol returning:
- presentedItemURL: the desired URL of the .srt file
- primaryPresentedItemURL: the URL of the video file for which I'd like to save the subtitles
then the code that should write the file is (I tried pretty much all the methods supported by NSFileCoordinator):
let presenter: NSFilePresenter <= class implementing the NSFilePresenter protocol given as input to the function
let data: NSData <= data containing the subtitle given as an input to the function
var error: NSError? = nil
let coord = NSFileCoordinator(filePresenter: presenter)
coord.coordinateReadingItemAtURL(presenter.presentedItemURL!, options: NSFileCoordinatorReadingOptions(), writingItemAtURL: presenter.presentedItemURL!, options: NSFileCoordinatorWritingOptions(), error: &error, byAccessor: { (readUrl: NSURL!, writeURL: NSURL!) in
print("attempt to write file: \(presenter.primaryPresentedItemURL!!.path)")
data?.writeToFile(gerSubtitlesFilePath(path, format: format), atomically: true)
return
})
the block is executed (I can see the print in the logs) but no file is written nor any error or warning is rised
I tried as well all the various coord.coordinateWritingItemAtURL methods as well witout success
What am I missing?