具有备用文件名的UIActivityViewController?

时间:2013-11-13 16:50:06

标签: ios iphone uiactivityviewcontroller mfmessagecomposeview uiactivity

我正在通过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备用文件名?

3 个答案:

答案 0 :(得分:4)

您可以使用临时目录中的任何名称创建指向该文件的硬链接(这样您就不必复制实际文件)并将其传递给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];

希望这有帮助!祝你好运!

答案 1 :(得分:1)

不,不可能。您是否可以在共享文件之前重命名该文件?

答案 2 :(得分:0)

很好,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
        }
    }