gpt4 book ai didi

ios - 从 iCloud Document Picker 附加 iWork 文档不起作用,因为它们是目录而不是文件

转载 作者:行者123 更新时间:2023-12-01 18:13:10 26 4
gpt4 key购买 nike

将任何 iWork 文档(PagesKeynoteNumbers)转换为 NSData来自 UIDocumentPickerViewController似乎不起作用,因为它们不是文件而是目录。

这是我的代码:

NSString *fileName = [[url path] lastPathComponent];
NSData *fileData = [[NSFileManager defaultManager] contentsAtPath:[docPickerUrl path]];
NSString *fileExtension = [docPickerUrl pathExtension];
NSString *UTI = (__bridge_transfer NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)fileExtension, NULL);
NSString *contentType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)UTI, kUTTagClassMIMEType);

文件数据始终为 nil自从 NSFileManager无法将目录转换为数据。

我得到 url来自 - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url

最佳答案

我建议通过 NSFileCoordinator 对象,这是访问安全范围文件(例如通过 Dropbox 和 iCloud 的文件)所需的 AFAIK。 (在这种情况下,取消注释下面代码中的第一行和倒数第二行。)options NSFIleCoordinator 的 coordinateReadingItemAtURL: 所需的参数是 NSFileCoordinatorReadingForUploading .这将正常读取单个文件,但会自动将目录转换为 zip 文件。脱掉.zip添加后,您将拥有一个有效的 Pages/Numbers/Keynote 文件。 (它也有效。)

- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {
//[url startAccessingSecurityScopedResource]; // Necessary for security-scoped files
NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] init];
NSError *error;
__block NSData *fileData;

[coordinator coordinateReadingItemAtURL:url options:NSFileCoordinatorReadingForUploading error:&error byAccessor:^(NSURL *newURL) {
// File name for use in writing the file out later
NSString *fileName = [newURL lastPathComponent];
NSString *fileExtension = [newURL pathExtension];

// iWork files will be in the form 'Filename.pages.zip'
if([fileExtension isEqualToString:@"zip"]) {
if([[[newURL URLByDeletingPathExtension] pathExtension] isEqualToString:@"pages"] ||
[[[newURL URLByDeletingPathExtension] pathExtension] isEqualToString:@"numbers"] ||
[[[newURL URLByDeletingPathExtension] pathExtension] isEqualToString:@"key"] ) {
// Remove .zip if it is an iWork file
fileExtension = [[newURL URLByDeletingPathExtension] pathExtension];
fileName = [[newURL URLByDeletingPathExtension] lastPathComponent];
}
}

NSError *fileConversionError;
fileData = [NSData dataWithContentsOfURL:newURL options:NSDataReadingUncached error:&fileConversionError];

// Do something with the file data here

}
//[url stopAccessingSecurityScopedResource]; // Necessary for security-scoped files
}

此处有关 NSFileCoordinator 选项的相关 Apple 文档:
https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSFileCoordinator_class/#//apple_ref/c/tdef/NSFileCoordinatorReadingOptions

关于ios - 从 iCloud Document Picker 附加 iWork 文档不起作用,因为它们是目录而不是文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26458592/

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