如何在 IOS 中使用应用程序打开下载的文件?

How to open downloaded file with app in IOS?

我正在开发一个应用程序,我在其中下载了各种格式的数据,例如(.pdf、.txt、.mp3 等)我已经使用了代码

NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,recFilName]; [nsdataFromBase64String writeToFile:filePath atomically:YES];

我使用上面的代码成功下载了,现在我必须使用支持该扩展的适当应用程序打开下载的文件,任何人都可以帮助我使用代码。

您需要创建 UIDocumentPickerViewController 的实例并实现其委托方法。

UIDocumentMenuViewController *documentPicker =
[[UIDocumentMenuViewController alloc] initWithDocumentTypes:@[<items>,...]
            inMode:UIDocumentPickerModeOpen];
documentPicker.delegate = self;
[self presentViewController:documentPicker animated:YES completion:nil];

文档选择器允许用户select 应用程序沙盒之外的项目。如果您正在下载文件并将其存储在本地文档目录中,请参阅下面的代码 ..

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
// Suppose you want to open downloaded PDF file
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"YourPDFFileName"];

NSURL *targetURL = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];

// Now load this request in WebView.

同样您也可以打开其他格式。