1

I am making an app that I want it to pick a song from the iPod music files then add it to the bundle files (documents folder) so that I can use it with the need to access the phones music list again.

I have managed to pick the song and get its ID but I don't know how to add a file to my bundles files using Swift.

I have found answers a bit similar to this question but its all since iOS 4 and using Objective C not Swift.

This is the code which has the files ID:

let u: NSURL? = 
        self.mediaItem!.valueForProperty(MPMediaItemPropertyAssetURL) as! NSURL?
if u == nil {
    return
}
Floern
  • 33,559
  • 24
  • 104
  • 119
Loay Elshall
  • 11
  • 1
  • 3

1 Answers1

0

The source of my answer is here, I didn't test the code:

func mediaPicker(mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
    let item: MPMediaItem = mediaItemCollection.items[0]
    let pathURL: NSURL? = item.valueForProperty(MPMediaItemPropertyAssetURL) as? NSURL
    if pathURL == nil {
        Alert.showPopupWithMessage("Unable to read DRM protected file.")
        return
    }

    // Export the ipod library as .m4a file to local directory for remote upload
    let exportSession = AVAssetExportSession(asset: AVAsset(URL: pathURL!), presetName: AVAssetExportPresetAppleM4A)
    exportSession?.shouldOptimizeForNetworkUse = true
    exportSession?.outputFileType = AVFileTypeAppleM4A
    let fileUrl = //Where you want to save the file
    exportSession?.outputURL = fileUrl
    exportSession?.exportAsynchronouslyWithCompletionHandler({ () -> Void in
        if exportSession!.status != AVAssetExportSessionStatus.Completed  {
            print("Export error")
        }
    })
}
Community
  • 1
  • 1
Marco Santarossa
  • 4,058
  • 1
  • 29
  • 49
  • ok so that answer half of my question but the other half is what is the code used to access documents folder in the bundle or accessing the bundle it self and saving files to it? – Loay Elshall Aug 28 '16 at 14:00