UIActivityViewController With Alternate Filename?
Asked Answered
T

3

11

I am sharing an audio recording via the UIActivityViewController. When the audio file shares via email or iMessage, it shows the underlying name of the audio file without any apparent way of changing 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 don't use the UIActivityViewController and just use MFMessageComposeViewController directly, than I can use

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

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

Thorp answered 13/11, 2013 at 16:50 Comment(1)
I would use NSFileManager to create a copy with desired name before adding as attachment. But there will be of course the problem how to handle the copy after finished using the controllerDistich
T
11

You can create a hard link to the file (so that you don't have 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 like this:

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!

Trichocyst answered 3/6, 2017 at 11:32 Comment(0)
H
9

Very nice, timaktimak. Thank you. Here is the same in Swift:

    private func createLinkToFile(atURL fileURL: URL, withName fileName: String) -> URL? {
        let fileManager = FileManager.default               // the default file maneger
        let tempDirectoryURL = fileManager.temporaryDirectory   // get the temp directory
        let linkURL = tempDirectoryURL.appendingPathComponent(fileName) // and append the new file name
        do {                                                // try the operations
            if fileManager.fileExists(atPath: linkURL.path) {   // there is already a hard link with that name
                try fileManager.removeItem(at: linkURL)     // get rid of it
            }
            try fileManager.linkItem(at: fileURL, to: linkURL)  // create the hard link
            return linkURL                                  // and return it
        } catch let error as NSError {                      // something wrong
            print("\(error)")                               // debug print out
            return nil                                      // and signal to caller
        }
    }
Hauge answered 29/9, 2020 at 9:11 Comment(0)
B
-1

No, not possible. Could you not just rename the file before sharing it?

Bagel answered 30/11, 2013 at 21:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.