Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIActivityViewController With Alternate Filename?

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?

like image 598
daveMac Avatar asked Nov 13 '13 16:11

daveMac


2 Answers

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!

like image 92
timaktimak Avatar answered Nov 15 '22 08:11

timaktimak


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
        }
    }
like image 41
FPP Avatar answered Nov 15 '22 08:11

FPP