Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i use UIDocumentInteractionController with MPMediaPickerController

I implemented MPMediaPickerController

- (IBAction)pickSong:(id)sender {
    MPMediaPickerController *picker =
    [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeMusic];

    picker.delegate                     = self;
    picker.allowsPickingMultipleItems   = NO;
    picker.prompt                       = NSLocalizedString (@"Select any song from the list", @"Prompt to user to choose some songs to play");
    picker.showsCloudItems = YES;

    [self presentModalViewController: picker animated: YES];
}
- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection
{
    [self dismissModalViewControllerAnimated: YES];
    if (mediaItemCollection.count > 0) {
        MPMediaItem *mediaItem = [mediaItemCollection.items objectAtIndex:0];
        NSLog(@"%@ - %@, %@", [mediaItem valueForProperty:MPMediaItemPropertyTitle], [mediaItem valueForProperty:MPMediaItemPropertyArtist], [mediaItem valueForProperty:MPMediaItemPropertyAssetURL]);


        self.fileURL = [mediaItem valueForProperty:MPMediaItemPropertyAssetURL];
    }
}

- (void) mediaPickerDidCancel: (MPMediaPickerController *) mediaPicker
{
    [self dismissModalViewControllerAnimated: YES];
}

and I also implemented UIDocumentInteractionController separately to be able to open a song in any application that is able to handle it

- (IBAction)shareButtonPressed:(id)sender {
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"Song.mp3" withExtension:nil];
    self.docController= [UIDocumentInteractionController interactionControllerWithURL:url];
    [self.docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}

now what I am trying to do is to merge the two of them, I want to share the song that is picked from the user, this is what I saved the song's URL self.fileURL = [mediaItem valueForProperty:MPMediaItemPropertyAssetURL]; and I have made a simple change to the share method

- (IBAction)shareButtonPressed:(id)sender {
    NSURL *url = self.fileURL;
    self.docController= [UIDocumentInteractionController interactionControllerWithURL:url];
    [self.docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}

I thought this would work but instead what I am getting is an error saying

* Assertion failure in -[UIDocumentInteractionController setURL:], /SourceCache/UIKit/UIKit-2903.23/UIDocumentInteractionController.m:1010 2013-10-28 03:14:10.294 ShareTest[1567:60b] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UIDocumentInteractionController: invalid scheme ipod-library. Only the file scheme is supported.'

the problem is clearly with the URL, I didn't implement it well, or I think a solution for that would be to take the NSData of the MPMediaItem and save it as .mp3 file in my app, but I don't know how to achieve this as well

Thank you in advance

like image 709
Alexy Ibrahim Avatar asked Oct 28 '13 01:10

Alexy Ibrahim


1 Answers

The problem is the kind of URL you have passed as a parameter.

You need to use this method: [NSURL fileURLWithPath:path] to create the path for the UIDocumentInteractionController.

The URL scheme you created is ipod-library, but needs to be file

like image 190
Velociround Avatar answered Sep 27 '22 19:09

Velociround