Reputation: 991
I'm displaying contents of the tmp directory in a UITableView, but cannot get the filepath for each cell.
When the cell is pressed, my app should play the file with MPMoviePlayer but is not.
This is what I have so far:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *tmpDirectory = NSTemporaryDirectory();
fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:tmpDirectory error:nil]; //nsarray
NSString *movieURL = [fileList objectAtIndex:indexPath.row];
NSURL *url = [NSURL URLWithString:movieURL];
_moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentModalViewController:_moviePlayerViewController animated:YES];
}
NSlog gives me the name of the video, but not the path. I feel like I'm over thinking this from lack of sleep.
Upvotes: 0
Views: 246
Reputation: 797
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let tmpDirectory: String = NSTemporaryDirectory()
print("\(tmpDirectory)")
var fileList: [Any]? = try? FileManager.default.contentsOfDirectory(atPath: tmpDirectory)
//nsarray
let movieURL: String? = (fileList?[indexPath.row] as? String)
let path: String = URL(fileURLWithPath: tmpDirectory).appendingPathComponent(movieURL!).absoluteString
print(path)
}
Upvotes: 1
Reputation: 7333
Change your code like:
NSString *tmpDirectory = NSTemporaryDirectory();
NSLog(@"%@",tmpDirectory);
NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:tmpDirectory error:nil]; //nsarray
NSString *movieURL = [fileList objectAtIndex:indexPath.row];
NSString *path=[tmpDirectory stringByAppendingPathComponent:movieURL];
Upvotes: 1
Reputation: 107231
When you use this:
NSString *movieURL = [fileList objectAtIndex:indexPath.row];
It'll return only the file name inside the temporary directory, So use this:
NSString *movieURL = [tmpDirectory stringByAppendingPathComponent:[fileList objectAtIndex:indexPath.row]];
NSURL*url = [NSURL fileURLWithPath:movieURL];
Upvotes: 2