UIActivityViewController与备用文件名? [英] UIActivityViewController With Alternate Filename?

查看:215
本文介绍了UIActivityViewController与备用文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过UIActivityViewController分享录音.通过电子邮件或iMessage共享音频文件时,它会显示音频文件的基础名称,而无需任何明显的更改方式.

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];

如果我不使用UIActivityViewController而是直接使用MFMessageComposeViewController,那么我可以使用

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"];

是否可以在UIActivityViewController中使用备用文件名?

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

推荐答案

您可以在临时目录中使用所需名称创建指向该文件的硬链接(这样就不必复制实际文件).将其传递给UIActivityViewController而不是文件.

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;
}

像这样使用它:

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!

这篇关于UIActivityViewController与备用文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆