我的应用程序 -> 任何文件夹中,则无法读取 fileURL 处的文件(因此无法上传到-6ren">
gpt4 book ai didi

ios - 文件提供程序扩展,importDocumentAtURL::无法读取给定 URL 处的文件 (iOS 11.4.1)

转载 作者:行者123 更新时间:2023-11-28 23:49:26 30 4
gpt4 key购买 nike

我在将操作粘贴到文件提供程序扩展中的容器中时遇到问题。

如果我将复制的图像或文本粘贴到"file"应用程序 -> 我的应用程序 -> 任何文件夹中,则无法读取 fileURL 处的文件(因此无法上传到我的服务器或存储在本地)。

- (void)importDocumentAtURL:(NSURL *)fileURL
toParentItemIdentifier:(NSFileProviderItemIdentifier)parentItemIdentifier
completionHandler:(void (^)(NSFileProviderItem _Nullable importedDocumentItem, NSError * _Nullable error))completionHandler
{

NSError *readError = nil;
NSData *fileData = [NSData dataWithContentsOfURL:fileURL options:NSDataReadingMappedAlways error:&readError];
NSString *readErrorMessage = readError.localizedDescription;

NSURL *myFileURL = [NSFileProviderManager.defaultManager.documentStorageURL URLByAppendingPathComponent:@"temp.dat"];
NSError *copyError = nil;
BOOL copyResult = [_fileManager copyItemAtURL:fileURL toURL:myFileURL error:&copyError];
NSString *copyErrorMessage = copyError.localizedDescription;

...

readErrorMessage 和 copyErrorMessage 都是:

无法打开文件“text.txt”,因为您无权查看该文件。

我在这里做错了什么?

谢谢。

UPD:从我的容器、iCloud 容器复制的任何文件以及从系统剪贴板中的文本/图像/其他数据生成的合成文件都会发生这种情况。

最佳答案

看起来您正在处理安全范围的 URL。

根据 Document Picker Programming Guide

Any app that accesses documents outside its sandbox must meet the following requirements:

  • Your app must perform all file read and write operations using file coordination.

  • If you display the contents of the document to the user, you must track the document’s state using a file presenter. If you’re only showing a list of files, a file presenter is not necessary.

  • Do not save any URLs accessed through open or move operations. Always open the document using a document picker, a metadata query, or a security-scoped bookmark to the URL.

  • These operations return security-scoped URLs. You must call startAccessingSecurityScopedResource before accessing the URL.

  • If startAccessingSecurityScopedResource returns YES, call stopAccessingSecurityScopedResource when you are done using the file.

  • If you are using a UIDocument subclass, it will automatically consume the security-scoped URLs for you. There’s no need to call startAccessingSecurityScopedResource or stopAccessingSecurityScopedResource. UIDocument also acts as a file presenter and automatically handles file coordination. For these reasons, using a UIDocument subclass is highly recommended for all files outside your app’s sandbox.

所以你需要调用startAccessingSecurityScopedResource在复制此 url 上的文件之前。您的代码可能会变成。

- (void)importDocumentAtURL:(NSURL *)fileURL
toParentItemIdentifier:(NSFileProviderItemIdentifier)parentItemIdentifier
completionHandler:(void (^)(NSFileProviderItem _Nullable importedDocumentItem, NSError * _Nullable error))completionHandler
{

NSError *readError = nil;
NSData *fileData = [NSData dataWithContentsOfURL:fileURL options:NSDataReadingMappedAlways error:&readError];
NSString *readErrorMessage = readError.localizedDescription;

NSURL *myFileURL = [NSFileProviderManager.defaultManager.documentStorageURL URLByAppendingPathComponent:@"temp.dat"];

// Call |startAccessingSecurityScopedResource| before working on the url
[fileURL startAccessingSecurityScopedResource];

NSError *copyError = nil;
BOOL copyResult = [_fileManager copyItemAtURL:fileURL toURL:myFileURL error:&copyError];
NSString *copyErrorMessage = copyError.localizedDescription;

// ....
// Call |stopAccessingSecurityScopedResource| after everything is done.
[fileURL stopAccessingSecurityScopedResource];
}

关于ios - 文件提供程序扩展,importDocumentAtURL::无法读取给定 URL 处的文件 (iOS 11.4.1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52198715/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com