【问题标题】:How to open downloaded file with app in IOS?如何在IOS中使用应用程序打开下载的文件?
【发布时间】:2016-08-01 10:30:12
【问题描述】:

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

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

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

【问题讨论】:

标签: ios ios7 ios5


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    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.
    

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

    【讨论】:

    • 我尝试了这种方法,但 .3gp 扩展在 web view.pdf 中无法正常工作。
    • 您可以在媒体播放器中打开该 3GP 视频文件。请参阅以下内容可能会对您有所帮助.. stackoverflow.com/questions/1535836/… 您现在必须使用 AVPlayer,因为不推荐使用 MPMoviePlayerController... 在本地存储 3gp 文件并使用 AVPlayer 打开它.. 希望这会有所帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多