저는 최근에 iPhone 파일 시스템을 가지고 놀기 시작했습니다. 처음에는 몇 가지 개념을 이해하는 데 어려움이 있었지만 결국에는 직선적으로 흐르게되었습니다.Documents 디렉토리로 파일을 복사하면 오류가 발생합니다.
내 문제는 application:openURL:sourceApplication:annotation:
메서드를 문서 디렉토리 (NSDocumentDirectory
)로 구현하여 메일 응용 프로그램 (또는 내 응용 프로그램에서 지원하는 파일 형식으로 내 응용 프로그램을 여는 응용 프로그램)에서 파일을 복사하려고하지만 내 이 일을 할 때 앱이 실패한 것 같습니다.
파일을 복사 할 때 파일 관리자가 반환하는 값 ([fileManager copyItenAtPath:filePath toPath:newFilePath error:&error];
, newFilePath은 내 문서 디렉토리)을 가지고값을 생성 한 다음이를 확인한 다음 실패 또는 성공한 경우 NSLog를 통해 항상 실패합니다.
내 문서 디렉토리에서 무슨 일이 일어나고 있는지 알아보기 위해 파일 공유를 활성화하고 복사 한 파일을 보는 대신받은 편지함 폴더를 얻었으며 해당 폴더를 데스크탑으로 복사하면 내 파일이 거기에 있습니다. 왜 이런 행동입니까? 내가 원하는 결과를 얻으려면 어떻게해야합니까?
애플 문서에서 UIDocumentInteractionController
파일을 열면받은 편지함 폴더로 복사되지만, 분명히 내 앱은 UIDocumentInteractionController
을 사용하지 않습니다.
미리 감사드립니다. 도움을 주시면 대단히 감사하겠습니다.
편집 여기 여기
을 시작합니다 내 원래의 코드입니다 :
//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]);
}
}
}
:이 모든 작업 파일에 수행하는 클래스가//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;
}
더 명확하게 물건을 설정합니다.
실패하면 어떤 오류 메시지가 나타 납니까? – Jim
아무 것도 아니요, 방금'BOOL' 결과가'YES' 인 경우'NSLog (@ "success");를 작성하고, 그렇지 않으면 "NSLog (@") 오류 "); ' – ArturoVM
'[error localalizedDescription]'을 사용하여 오류를 기록 할 수 있습니까? – FreeAsInBeer