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

时间:2014-10-20 04:29:56

标签: ios cocoa-touch icloud

将{i} 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);

fileData始终为nil,因为NSFileManager无法将目录转换为数据。

我从url

获得- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url

3 个答案:

答案 0 :(得分:2)

我建议通过NSFileCoordinator对象,这是访问安全范围文件所需的AFAIK,例如通过Dropbox和iCloud。 (在这种情况下,取消注释下面代码中的第一行和倒数第二行。)NSFIleCoordinator的options所需的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

答案 1 :(得分:1)

我在这里遇到同样的问题:iOS8: UIDocumentPickerViewController get NSData

解决方案是将iWork文档添加为zip存档,最简单的方法是使用SSZipArchive库,也可以作为cocoa pod使用:

pod 'SSZipArchive', '~> 0.3'

答案 2 :(得分:0)

你有没有得到这个,我已经尝试使用像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

} }