简体   繁体   English

在 IOS 中通过 UIActivityViewController 共享 PDF base64 字符串时如何重命名文件名

[英]How I can rename file name when I share PDF base64 string via UIActivityViewController in IOS

I got PDF base64 string from server, It looks like this:我从服务器得到了 PDF base64 字符串,它看起来像这样:

"data:application/pdf;base64,JVBERi0x...." “数据:应用程序/pdf;base64,JVBERi0x ....”

Now I would like to share this file.现在我想分享这个文件。 I am using Cordova social share plugin, it works well.我正在使用 Cordova 社交分享插件,效果很好。 But how can I assign the file name?但是我如何分配文件名? now, it only shows default name like " PDF document.pdf "现在,它只显示默认名称,如“ PDF document.pdf

//activityItems is the base64 string
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:applicationActivities];

在此处输入图片说明

You have two option to do this functionality. 您有两个选择可以执行此功能。

1) You can Rename your file in Document Directory. 1)您可以在文档目录中重命名文件。

2) You can change your file name when you are going to save it in Document Directory. 2)要将文件名保存在文档目录中时,可以更改文件名。

1) How to change the filename in Document Directory: 1)如何在文件目录中更改文件名:

- (BOOL)renameFileFrom:(NSString*)oldName to:(NSString *)newName
{
    NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                   NSUserDomainMask, YES) objectAtIndex:0];
    NSString *oldPath = [documentDir stringByAppendingPathComponent:oldName];
    NSString *newPath = [documentDir stringByAppendingPathComponent:newName];

    NSFileManager *fileMan = [NSFileManager defaultManager];
    NSError *error = nil;
    if (![fileMan moveItemAtPath:oldPath toPath:newPath error:&error])
    {
        NSLog(@"Failed to move '%@' to '%@': %@", oldPath, newPath, [error localizedDescription]);
        return NO;
    }
    return YES;
}

2) How to change the filename at the time of save File into Document Directory: 2)在将文件保存到文档目录时如何更改文件名:

- (NSURL *)applicationDocumentsDirectory {
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory 
         inDomains:NSUserDomainMask] lastObject];
}

NSString *path = [[self applicationDocumentsDirectory].path 
                       stringByAppendingPathComponent:@"fileName.pdf"];
[sampleText writeToFile:path atomically:YES
                       encoding:NSUTF8StringEncoding error:nil];

Save the file from URL to a temp directory and then use it in the UIActivityViewController将文件从 URL 保存到临时目录,然后在 UIActivityViewController 中使用它

let temporaryDir = FileManager.default.temporaryDirectory
let tempFilePath = temporaryDir.appendingPathComponent(pass the file name you want to show here)
do {
    try? Data(contentsOf: pass the file URL here).write(to: tempFilePath, options: .atomic)
  }
  
let activityViewController = UIActivityViewController(activityItems:  [tempFilePath], applicationActivities: nil)
present(activityViewController, animated: true, completion: nil)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM