7

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

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,那么我可以使用

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

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

4

3 回答 3

11

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

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

希望这可以帮助!祝你好运!

于 2017-06-03T11:32:24.487 回答
5

非常好,timaktimak。谢谢你。在 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
        }
    }
于 2020-09-29T09:11:15.260 回答
-1

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

于 2013-11-30T21:40:35.000 回答