UIActivityViewController with an alternate name?

I use audio recording through the UIActivityViewController. When an audio file is shared by email or iMessage, it shows the base name of the audio file without a visible way to change it.

NSArray *activityItems = [NSArray arrayWithObjects:self.audioPlayer.url, nil]; UIActivityViewController *avc = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil]; [self presentViewController:avc animated:YES completion:nil]; 

If I do not use the UIActivityViewController and just use the MFMessageComposeViewController directly, than I can use

 [composer addAttachmentURL:self.audioPlayer.url withAlternateFilename:@"Piano Song.m4a"]; 

Is it possible to have an alternate file name using the UIActivityViewController?

0
source share
2 answers

You can create a hard link to the file (so you don’t need to copy the actual file) with any name you want in the temporary directory and pass it to the UIActivityViewController instead of the file.

 - (NSURL *)createLinkToFileAtURL:(NSURL *)fileURL withName:(NSString *)fileName { NSFileManager *fileManager = [NSFileManager defaultManager]; // create a path in the temp directory with the fileName NSURL *tempDirectoryURL = [[NSFileManager defaultManager] temporaryDirectory]; NSURL *linkURL = [tempDirectoryURL URLByAppendingPathComponent:fileName]; // if the link already exists, delete it if ([fileManager fileExistsAtPath:linkURL.path]) { NSError *error; [fileManager removeItemAtURL:linkURL error:&error]; if (error) { // handle the error } } // create a link to the file NSError *error; BOOL flag = [fileManager linkItemAtURL:fileURL toURL:linkURL error:&error]; if (!flag || error) { // handle the error } return linkURL; } 

Use it as follows:

 NSURL *fileURL = ...; NSString *desiredName = ...; NSURL *linkURL = [self createLinkToFileAtURL:fileURL withName:desiredName]; UIActivityViewController *viewController = [[UIActivityViewController alloc] initWithActivityItems:@[linkURL] applicationActivities:nil]; [self presentViewController:viewController animated:YES completion:nil]; 

Hope this helps! Good luck

+1
source

No, It is Immpossible. Could you just rename the file before sharing it?

0
source

Source: https://habr.com/ru/post/1444338/


All Articles