从iCloud文档选取器附加iWork文档不起作用,因为它们是目录而不是文件
将任何iWork文档( Pages
, Keynote
, Numbers
)从UIDocumentPickerViewController
转换成NSData
似乎并不奏效,因为它们不是文件而是目录。
这是我的代码:
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);
fileData总是nil
因为NSFileManager
不能将目录转换为数据。
我从- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url
我在这里有同样的问题: iOS8:UIDocumentPickerViewController获取NSData
解决scheme是将iWork文档添加为zip压缩文件,最简单的方法是使用SSZipArchive库,也可以作为cocoapod使用:
pod 'SSZipArchive', '~> 0.3'
我build议通过一个NSFileCoordinator对象,这个对象是通过Dropbox和iCloud访问安全范围文件所需的AFAIK。 (在这种情况下,取消注释下面代码中的第一行和倒数第二行)。NSFIleCoordinator的coordinateReadingItemAtURL:
所需的options
参数是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 }
相关Apple文档NSFileCoordinator选项在这里: https : //developer.apple.com/library/ios/documentation/Foundation/Reference/NSFileCoordinator_class/#//apple_ref/c/tdef/NSFileCoordinatorReadingOptions
你有没有得到任何与此,我已经尝试使用像SSZipArchive或ZipArchive的东西压缩包,但得到一个崩溃。
这是我得到哪些可能有助于点燃一些想法的地方。
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url { if (controller.documentPickerMode == UIDocumentPickerModeImport) { NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:url includingPropertiesForKeys:[NSArray arrayWithObject:NSURLNameKey] options:0 error:nil]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *newDocDirectory = [paths objectAtIndex:0]; NSString *docDirectory = [dirContents objectAtIndex:0]; BOOL isDir=NO; NSArray *subpaths; NSString *exportPath = docDirectory; NSFileManager *fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath:exportPath isDirectory:&isDir] && isDir){ subpaths = [fileManager subpathsAtPath:exportPath]; } NSString *archivePath = [newDocDirectory stringByAppendingString:@".zip"]; ZipArchive *archiver = [[ZipArchive alloc] init]; [archiver CreateZipFile2:archivePath]; for(NSString *path in subpaths) { NSString *longPath = [exportPath stringByAppendingPathComponent:path]; if([fileManager fileExistsAtPath:longPath isDirectory:&isDir] && !isDir) { NSLog(@"adding file %@", path); [archiver addFileToZip:longPath newname:path]; } } NSData *documentData = [NSData dataWithContentsOfFile:archivePath]; //post documentDate as zip file to server
}}
- 我可以做NSVariableFromString像NSClassFromString和NSSelectorFromString?
- 和UIGestureRecognizer一起听UITouch事件
- 如何检测文本字段中是否有文本而不是数字值?
- 我如何find从辅助线程调用UIKit实例?
- 将行添加到现有的UITableView部分
- iPhone如何保存视图状态
- NSDate格式化问题与Twitter的JSON API
- 当它调用“presentViewController:”时,UIAlertController被移动到屏幕顶部的马车位置
- 如何使用UINavigationController使用UIViewControllerAnimatedTransitioning?