wait till media selection in iPhone

107 views Asked by At

I am working with the MPMediaPickerController controller.

Right now I am able to select an item from the gallery using the following code:

MPMediaPickerController *controller = [[MPMediaPickerController alloc]initWithMediaTypes:MPMediaTypeAnyAudio];
controller.delegate = mediaDelegate._pickerDelegate;
[self presentViewController:controller animated:YES completion:nil];
NSLog(@"url :%@",singleton.url);

The intention is that my method should return the URL of the selected item. Instead I am getting null for the URL because the line below the present view controller gets called as soon as view controller presented (it is not waiting for selection of item).

Is there any way to stop after presentViewController, for selection of item and then return the selected item URL from the method ?

2

There are 2 answers

2
Antonio MG On

You need to implement the MPMediaPickerControllerDelegate delegate:

- (void)mediaPicker: (MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection

When the user selects an item, that method will be called

3
AudioBubble On

You have to write this

[self presentViewController:controller animated:YES completion:nil];
NSLog(@"url :%@",singleton.url);

in

- (void)mediaPicker: (MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection

method..