我最近开始使用iPhone文件系统,起初我在理解某些概念时遇到了一些麻烦,但我认为最终我让它们变得直线流畅。将文件复制到文档目录导致失败
我的问题是,我试图复制从邮件应用程序文件(或打开我的应用程序与文件类型我的应用程序支持的任何应用程序)通过实施application:openURL:sourceApplication:annotation:
方法文档目录(NSDocumentDirectory
),但我的应用程序似乎在执行此操作时失败。
我创建了一个BOOL
与我的文件管理器在复制文件时返回的值([fileManager copyItenAtPath:filePath toPath:newFilePath error:&error];
,其中newFilePath是我的文档目录),然后检查它,然后NSLog如果失败或成功,我总是失败。
在试图找出我的Documents目录发生了什么后,我启用了文件共享,而不是看到我复制的文件,我得到一个收件箱文件夹,并将该文件夹复制到桌面后,我看到我的文件在那里。这是为什么?我怎样才能得到我想要的结果?
我在苹果文档的某个地方看到,当UIDocumentInteractionController
打开一个文件时,它将它复制到收件箱文件夹,但显然,我的应用程序不使用UIDocumentInteractionController
。
预先感谢您的帮助,非常感谢。
编辑从这里开始
这里是我的原代码:
//AppDelegate.m
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication (NSString *)sourceApplication annotation:(id)annotation
{
rootFolder.fileURLFromLaunch = url; //Passing the URL on to aproperty on a different class
[[NSNotificationCenter defaultCenter] postNotificationName:@"ApplicationDidOpenFile" object:nil]; // notifying that the URL was passed and a file was opened
return YES;
}
这是做与文件中的所有工作类:
//RootFolder.m
- (void)copyFileIntoApp
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [fileURLFromLaunch path];
NSString *displayName = [filePath lastPathComponent];
NSError *error = NULL;
NSString *finalFilePath = [NSString stringWithFormat:@"%@/%@", documentsPath, displayName];
if ([fileManager fileExistsAtPath:finalFilePath]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"File already exists!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
} else {
BOOL success = [fileManager copyItemAtPath:filePath toPath:finalFilePath error:&error];
if (success) {
NSLog(@"success");
Document *document = (Document *) [NSEntityDescription insertNewObjectForEntityForName:@"Document" inManagedObjectContext:managedObjectContext];
[document setFilename:displayName];
[document setPath:finalFilePath];
if (![managedObjectContext save:&error]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Please try again or restart the app" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
[fileArray insertObject:document atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
} else {
NSLog(@"failure %@ ", [error localizedDescription]);
}
}
}
我希望这使事情更清楚。
失败时会得到什么错误信息? – Jim 2011-03-06 16:02:11
没什么,我只是创建了一个'if ... else'循环,其中如果'BOOL'结果'YES',我会执行'NSLog(@“success”);'如果结果为NO,我会执行'NSLog(@失败“);' – ArturoVM 2011-03-06 16:17:29
你能用'[error localalizedDescription]'记录错误吗? – FreeAsInBeer 2011-03-06 16:20:22