0

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 ?

johnkavanagh
  • 4,614
  • 2
  • 25
  • 37

2 Answers2

2

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

Antonio MG
  • 20,382
  • 3
  • 43
  • 62
  • i have already have that method . But before I select that method the control returns from the upper method from which I am calling [self presentViewController:controller animated:YES completion:nil]; and hence I am getting null url – user3057042 Dec 02 '13 at 10:52
  • I dont think you understand how a delegate works. You need to wait to the user to select that, and the delegate will be called, you cannot stop the run of your program in order to wait to a selection. – Antonio MG Dec 02 '13 at 11:00
0

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..

  • @user3057042: Because in your code you are present controller after immdtly of creation. you need to implement in delegate method. –  Dec 02 '13 at 11:00
  • So, when user select some item then after delegate method is called in and present your controller. –  Dec 02 '13 at 11:00
  • @user3057042: as Antonio MG said ,i dont think you understand how a delegate works. You need to wait to the user to select that, and the delegate will be called, you cannot stop the run of your program in order to wait to a selection. –  Dec 02 '13 at 11:03