[英]Cannot access file after moving it to documents directory
I have an App that uses CloudKit
as backend, which has some CKRecords
with some CKAssets
我有一个使用一个App CloudKit
作为后端,其中有一些CKRecords
一些CKAssets
Some of those assets are PDF
files, and I'm failing to open them in the QuickLook Previewer
其中一些资产是PDF
文件,但是我无法在QuickLook Previewer
打开它们
After fetching the records, I save the CKAsset
URLs
in an array, with the following code: 提取记录后,我将CKAsset
URLs
保存在数组中,并使用以下代码:
guard let assets = attachment["attachments"] as? [CKAsset] else { return }
assets.forEach({ (asset) in
attachmentsURLs.append(asset.fileURL)
})
Each fileURL
gets printed somehow like this: 每个fileURL
都以这种方式打印:
file:///private/var/mobile/Containers/Data/Application/C4406091-B55C-46AC-A612-69052F2275BB/Library/Caches/CloudKit/34af81edf8344be1cf473ef394e06ccc8e1bc8cf/Assets/85DEDDB6-FB5B-4BC9-AB93-D8951AC304F7.012af7f83e322df6d0d1829b94956e4b829edbe974
The last string : 85DEDDB6-FB5B-4BC9-AB93-D8951AC304F7.012af7f83e322df6d0d1829b94956e4b829edbe974
is the file itself, and having this I try to move this file to DocumentsDirectory
. 最后一个字符串: 85DEDDB6-FB5B-4BC9-AB93-D8951AC304F7.012af7f83e322df6d0d1829b94956e4b829edbe974
是文件本身,因此我尝试将此文件移至DocumentsDirectory
。
I try to move them and rename them and give them the specific extension, in this particular case will be .pdf
我尝试移动它们并重命名它们,并给它们指定特定的扩展名,在这种情况下,它将是.pdf
I do this with the following code: 我使用以下代码执行此操作:
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
var url = URL.init(fileURLWithPath: documentsPath)
let component = NSUUID().uuidString + "." + fileExtensions[index]
url.appendPathComponent(component)
do {
try FileManager.default.moveItem(at: fileURL as URL, to: url)
} catch {
print("Failed to save attachment with extension with error:", error)
return
}
As you can see, I move the cached file to documents directory and rename it with a random string + file extension. 如您所见,我将缓存的文件移到documents目录,并使用随机字符串+文件扩展名对其进行重命名。
Using the iOS Simulator, everything works as expected. 使用iOS模拟器,一切正常。
The file gets moved from here: file:///Users/ivancantarino/Library/Developer/CoreSimulator/Devices/CCE2FCA5-05F0-4BB7-9A25-CBC168398A62/data/Containers/Data/Application/561A3461-0048-4676-808F-2D96B74DBDCD/Library/Caches/CloudKit/34af81edf8344be1cf473ef394e06ccc8e1bc8cf/Assets/FADF0479-E7AE-4340-83C5-C9E0889E4E87.012af7f83e322df6d0d1829b94956e4b829edbe974
该文件从此处移到: file:///Users/ivancantarino/Library/Developer/CoreSimulator/Devices/CCE2FCA5-05F0-4BB7-9A25-CBC168398A62/data/Containers/Data/Application/561A3461-0048-4676-808F-2D96B74DBDCD/Library/Caches/CloudKit/34af81edf8344be1cf473ef394e06ccc8e1bc8cf/Assets/FADF0479-E7AE-4340-83C5-C9E0889E4E87.012af7f83e322df6d0d1829b94956e4b829edbe974
to here: 到这里:
file:///Users/ivancantarino/Library/Developer/CoreSimulator/Devices/CCE2FCA5-05F0-4BB7-9A25-CBC168398A62/data/Containers/Data/Application/561A3461-0048-4676-808F-2D96B74DBDCD/Documents/52CAC8CD-EF0E-4E55-8D1B-8B267D07BAAA.pdf
And them I fill the QuickLook
datasource and I can visualize the files as expected, however in a real iPhone device this doesn't work. 然后他们填充了QuickLook
数据源,并且可以按预期显示文件,但是在真正的iPhone设备中,这是行不通的。
The preview shows an empty file, with ZERO KB
. 预览显示一个带有ZERO KB
的空文件。
On a real device the file gets moved from here: 在真实设备上,文件从此处移出:
file:///private/var/mobile/Containers/Data/Application/6870DDC2-32FD-4FF2-89F4-04F87BBE534B/Library/Caches/CloudKit/34af81edf8344be1cf473ef394e06ccc8e1bc8cf/Assets/A187A987-BE4E-4C6A-9CBE-E66F3FEF7333.012af7f83e322df6d0d1829b94956e4b829edbe974
to here: 到这里:
file:///var/mobile/Containers/Data/Application/6870DDC2-32FD-4FF2-89F4-04F87BBE534B/Documents/FD501C6D-9E95-46DD-BC58-EA2C45B62DAC.pdf
What can be causing this issue? 是什么导致此问题?
Why does this works in the simulator and doesn't work on the device? 为什么这在模拟器中有效而在设备上不起作用?
I've tried a lot of different stuff and this is driving me crazy. 我尝试了很多不同的东西,这让我发疯。
Any hint? 有什么提示吗?
Thank you. 谢谢。
Well, I solved the issue and it was a bit embarrassing to be honest, and I'll explain. 好吧,我解决了这个问题,说实话有点尴尬,我会解释。
Since I'm using QuickLook
framework from Apple, which is pretty amazing in my opinion, I had to implement the QuickLook protocol
methods in order to fill the datasource
to be able to present the desired files, and the problem was right here. 由于我使用的是Apple的QuickLook
框架,我认为这非常了不起,因此我必须实现QuickLook protocol
方法才能填充datasource
,以便能够呈现所需的文件,而问题就在这里。
After fetching the desired files I saved the URLs
in a [URL]
array. 提取所需的文件后,我将URLs
保存在[URL]
数组中。
In the QuickLook protocol
method numberOfPreviewItems(:_)
I return the [URL].count
as the number of items and in the previewController(:_)->QLPreviewItem
I was doing the following: 在QuickLook protocol
方法numberOfPreviewItems(:_)
我返回[URL].count
作为项目数;在previewController(:_)->QLPreviewItem
我正在执行以下操作:
func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
let doc = URL(fileURLWithPath: attachmentURLs[index].absoluteString)
return doc as QLPreviewItem
}
But if you notice, the doc
variable was initiated with a URL(fileWithPath)
and I was passing the URL.absoluteString
which doesn't point to the specific file; 但是,如果您注意到, doc
变量是使用URL(fileWithPath)
初始化的,而我正在传递未指向特定文件的URL.absoluteString
。
Instead I converted that line to the following: 相反,我将该行转换为以下内容:
let doc = URL(fileURLWithPath: attachmentURLs[index].path)
Since I want to access the Path
of the given cached URL
, and by doing this everything worked perfectly. 由于我要访问给定缓存URL
的Path
,因此,一切正常。
What was an headache was that with the previous implemented way it worked in the iOS Simulator
but not on the real device, and that lead me to seek the answer in different ways. 令人头疼的是,采用以前的实现方式,它只能在iOS Simulator
而不能在真实设备上运行,这使我以不同的方式寻求答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.