问题
Converting any iWork documents (Pages
, Keynote
, Numbers
) into NSData
from the UIDocumentPickerViewController
does not seem to be working since they are not files but directories.
Here is my code:
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 is always nil
since NSFileManager
can't convert directory to data.
I get url
from - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url
回答1:
I have the same issue here: iOS8: UIDocumentPickerViewController get NSData
The solution is to add iWork documents as zip archives, the simplest way is to use SSZipArchive library, also available as cocoa pod:
pod 'SSZipArchive', '~> 0.3'
回答2:
I recommend going through a NSFileCoordinator object, which is AFAIK required for accessing security-scoped files such as those through Dropbox and iCloud. (Uncomment the first and next-to-last lines in the code below in that case.) The options
parameter you want for NSFIleCoordinator's coordinateReadingItemAtURL:
is NSFileCoordinatorReadingForUploading
. This will read in single files normally, but will turn directories into zip files automatically. Strip off the .zip
that is added on and you'll have a valid Pages/Numbers/Keynote file. (It's valid with it on too.)
- (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
}
Relevant Apple documentation on the NSFileCoordinator options here: https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSFileCoordinator_class/#//apple_ref/c/tdef/NSFileCoordinatorReadingOptions
回答3:
Did you get anywhere with this, i've tried compressing the package using something like SSZipArchive or ZipArchive but get a crash.
This is where've i've got to which might help spark some ideas.
- (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
} }
来源:https://stackoverflow.com/questions/26458592/attaching-iwork-documents-from-icloud-document-picker-does-not-work-because-they