dav*_*Mac 0 iphone ios mfmessagecomposeviewcontroller uiactivity uiactivityviewcontroller
我正在通过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];
Run Code Online (Sandbox Code Playgroud)
如果我不使用UIActivityViewController而仅直接使用MFMessageComposeViewController,那么我可以使用
[composer addAttachmentURL:self.audioPlayer.url withAlternateFilename:@"Piano Song.m4a"];
Run Code Online (Sandbox Code Playgroud)
UIActivityViewController是否可以使用备用文件名?
非常好,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
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用临时目录中想要的任何名称来创建指向该文件的硬链接(这样就不必复制实际文件),并将其传递给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;
}
Run Code Online (Sandbox Code Playgroud)
像这样使用它:
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];
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!祝好运!
| 归档时间: |
|
| 查看次数: |
1427 次 |
| 最近记录: |