QLPreviewController works with URLs provided by UIDocumentBrowserViewController, but not with URLs generated manually

2.4k views Asked by At

I'm using UIDocumentBrowserViewController combined with QLPreviewController to preview documents selected by users in UIDocumentBrowserViewController, which works perfectly fine. The pickedDocumentURL variable used by QLPreviewController is populated as follows:


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

    pickedDocumentURL = documentURLs.first as NSURL?

    // Present QLPreviewController instance ...
}

However, when I populate the pickedDocumentURL variable using:

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

or:

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

... then the QLPreviewController does not work (it is presented, but the preview is empty) and I get the following error on the console:


[default] QLUbiquitousItemFetcher: could not create sandbox wrapper. Error: Error Domain=NSPOSIXErrorDomain Code=1 "couldn't issue sandbox extension com.apple.quicklook.readonly for '/private/var/mobile/Containers/Shared/AppGroup/07524B34-D877-449F-A5C3-89A0431560E5/File Provider Storage/22207162/1qrbGgy6-u0f69mPqOjjpzlYiUYPR8OG_/Sample.pdf': Operation not permitted" UserInfo={NSDescription=couldn't issue sandbox extension com.apple.quicklook.readonly for '/private/var/mobile/Containers/Shared/AppGroup/07524B34-D877-449F-A5C3-89A0431560E5/File Provider Storage/22207162/1qrbGgy6-u0f69mPqOjjpzlYiUYPR8OG_/Sample.pdf': Operation not permitted} #PreviewItem

Moreover, the URL absolute strings in each of those cases are exactly the same.

2

There are 2 answers

0
LanePcole On

Here's the code that works for me:

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
}
0
user173488 On

you are using .absoluteString, use .path instead, I had same issue and this solved it:

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