私は最近、iPhone のファイル システムをいじり始めました。最初は、いくつかの概念を理解するのに苦労しましたが、最終的には、それらをまっすぐに、スムーズに理解できたと思います。
私の問題は、application:openURL:sourceApplication:annotation:
メソッドをドキュメント ディレクトリ ( NSDocumentDirectory
) に実装することにより、メール アプリ (または、アプリがサポートするファイル タイプでアプリを開くアプリ) からファイルをコピーしようとしているのですが、実行するとアプリが失敗するようです。これ。
BOOL
ファイルをコピーするときにファイルマネージャーが返す値 ( newFilePath はドキュメントディレクトリ) を使用してを作成[fileManager copyItenAtPath:filePath toPath:newFilePath error:&error];
し、それをチェックしてから、失敗または成功した場合は NSLog を作成すると、常に失敗します。
Documents ディレクトリで何が起こっているのかを調べようとして、ファイル共有を有効にしました。コピーしたファイルが表示される代わりに、受信トレイ フォルダが表示され、そのフォルダをデスクトップにコピーすると、ファイルがそこにあることがわかります。 . この動作はなぜですか?希望する結果を得るにはどうすればよいですか?
Apple のドキュメントのどこかで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]);
}
}
}
物事がより明確になることを願っています。