0

私は最近、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]);
    }


}

}

物事がより明確になることを願っています。

4

2 に答える 2

1

インスタンス化時にエラーをnullに設定していますか?ポイントするメモリNSErrorがNULLでない場合、ロジックがトリップします。

これを行う:

NSError *error = NULL;

これではない:

NSError *error;
于 2011-03-06T15:54:14.317 に答える
1

Inbox フォルダを取得し、そのフォルダをデスクトップにコピーすると、ファイルがそこにあることがわかります。この動作はなぜですか?希望する結果を得るにはどうすればよいですか?

アプリはサンドボックス化されており、独自のディレクトリにないファイルにアクセスできないためです。ユーザーが特定のアプリでファイルを開くことを iOS に伝えると、iOS はそのファイルを inbox ディレクトリにコピーします。


の完全なコードを投稿してapplication:openURL:sourceApplication:annotation:ください。
あなたの「これとあれをやっている」はあまり役に立ちません。これとあれの間に欠陥があるからです。あなたの言葉で説明したアルゴリズムは正しいですが、実装には明らかにバグがあります。

したがって、このメソッドの完全なコードを投稿してください。


編集: コードの関連部分をテストしましたが、動作しているようです。

fileManagernil でないことを確認します。これにより、エラー メッセージが表示されない理由が説明されます。

于 2011-03-06T16:38:26.343 に答える