0

I am attempting to assign the song title, author, and seconds played of a song to a set of variables. However I am unable to pull any information from the song, what would be the best way to do this as currently my way crashes.

func presentPicker (sender:AnyObject) {
//I have all of this within an IBAction if that matters, I am new to programming in general so sorry if theres any stupid mistakes
    let mediaPicker = MPMediaPickerController(mediaTypes: .Music)
    mediaPicker.delegate = self
    mediaPicker.allowsPickingMultipleItems = false
    presentViewController(mediaPicker, animated: true, completion: {println(MPMediaItemCollection())})

}
Juan Catalan
  • 2,299
  • 1
  • 17
  • 23
  • What is the error message when it crashes? – Dash May 30 '15 at 04:37
  • which method, and I believe the crash is because completion simply means when the view controller is presented, which would be before a song has been chosen. – mattcarlson May 30 '15 at 04:38

1 Answers1

1

You shouldn't be using the completion argument of presentViewController:. The completion runs when the controller successfully presents, but you want to grab the song when it's finished dismissing. You need to implement this delegate method in your class:

func mediaPicker(mediaPicker: MPMediaPickerController!,
    didPickMediaItems mediaItemCollection: MPMediaItemCollection!) {
    println(mediaItemCollection)
}

That method will be called on your class when the user has selected a song because you set delegate equal to self. You may also want to implement this one to find out if they cancelled the picker:

func mediaPickerDidCancel(_ mediaPicker: MPMediaPickerController!)
Dash
  • 17,188
  • 6
  • 48
  • 49