Search code examples
iosaudiosharensdata

Pass audio file from document folder to activity controller


I have stored the recorded audios in the Document folder of the app. When I pass the recorded file to the activity controller, it always returns nil and I am getting the following error:

[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]

My code:

NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

NSString *path = [DOCUMENTS_FOLDER stringByAppendingPathComponent:[fileList objectAtIndex:[indexPath row]]];

NSURL *audioURL = [NSURL URLWithString:path];
NSData *audioData = [NSData dataWithContentsOfURL:audioURL];

self.activityViewController = [[UIActivityViewController alloc]

                               initWithActivityItems:@[audioData] applicationActivities:nil];

[self.activityViewController setValue:@"Recording" forKey:@"subject"];


[self presentViewController:self.activityViewController animated:YES completion:nil];

Any help?


Solution

  • Alright. Managed to resolve by changing from

    NSURL *audioURL = [NSURL URLWithString:path];
    NSData *audioData = [NSData dataWithContentsOfURL:audioURL];
    self.activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[audioData] applicationActivities:nil];
    

    to this

    NSURL *audioURL = [NSURL fileURLWithPath:path];
    self.activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[audioURL] applicationActivities:nil];
    

    Hope this helps other who has similar problem.