0

我正在使用 UIDocumentBrowserViewController 结合 QLPreviewController 来预览用户在 UIDocumentBrowserViewController 中选择的文档,效果很好。使用的pickedDocumentURL变量QLPreviewController填充如下:


func documentBrowser(_ controller: UIDocumentBrowserViewController, didPickDocumentURLs documentURLs: [URL]) {
    // (...)

    pickedDocumentURL = documentURLs.first as NSURL?

    // Present QLPreviewController instance ...
}

但是,当我使用以下方法填充pickedDocumentURL变量时:

pickedDocumentURL = NSURL(string: documentURLs.first!.absoluteString)

或者:

pickedDocumentURL = URL(string: documentURLs.first!.absoluteString) as NSURL?

...然后QLPreviewController不起作用(它已显示,但预览为空)并且我在控制台上收到以下错误:


[默认] QLUbiquitousItemFetcher:无法创建沙盒包装器。错误:错误域 = NSPOSIXErrorDomain 代码 = 1“无法为 '/private/var/mobile/Containers/Shared/AppGroup/07524B34-D877-449F-A5C3-89A0431560E5/文件提供程序发出沙盒扩展 com.apple.quicklook.readonly Storage/22207162/1qrbGgy6-u0f69mPqOjjpzlYiUYPR8OG_/Sample.pdf':不允许操作" UserInfo={NSDescription=couldn't issue sandbox extension com.apple.quicklook.readonly for '/private/var/mobile/Containers/Shared/AppGroup/ 07524B34-D877-449F-A5C3-89A0431560E5/文件提供程序存储/22207162/1qrbGgy6-u0f69mPqOjjpzlYiUYPR8OG_/Sample.pdf':不允许操作}#PreviewItem

此外,每种情况下的 URL 绝对字符串完全相同。

4

2 回答 2

0

你正在使用.absoluteString,改用.path,我有同样的问题,这解决了它:

pickedDocumentURL = NSURL(string: documentURLs.first!.path)
于 2019-04-26T07:13:10.883 回答
-1

这是对我有用的代码:

func getPreviewItem(withName name: String ) -> NSURL
{
    //let file = name.components(separatedBy: ".")
    let pdfFile = getDocumentsDirectory().appendingPathComponent(name)
    let url = pdfFile as NSURL (this line was the key)
    return url
}
于 2018-11-08T03:20:27.140 回答