58

ファイルをドキュメントディレクトリに保存するために、次のコードを設定しました。

NSLog(@"Saving File...");

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.reddexuk.com/logo.png"]];
AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"logo.png"];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Successfully downloaded file to %@", path);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

[operation start];

ただし、正常に保存されたら、各ファイルをUITableViewに追加したいと思います。UITableViewのファイルをタップしたら、UIWebViewでそのファイルに移動します(すべてオフライン)。

また、 http://www.reddexuk.com/logo.pngの代わりに「logo.png」などのファイル名と末尾を取得するにはどうすればよいですか?

これどうやってするの?

4

9 に答える 9

145

これは、ディレクトリのコンテンツを取得するために使用する方法です。

-(NSArray *)listFileAtPath:(NSString *)path
{
    //-----> LIST ALL FILES <-----//
    NSLog(@"LISTING ALL FILES FOUND");

    int count;

    NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
    for (count = 0; count < (int)[directoryContent count]; count++)
    {
        NSLog(@"File %d: %@", (count + 1), [directoryContent objectAtIndex:count]);
    }
    return directoryContent;
}
于 2011-12-04T16:08:27.810 に答える
21
-(NSArray *)findFiles:(NSString *)extension{

NSMutableArray *matches = [[NSMutableArray alloc]init];
NSFileManager *fManager = [NSFileManager defaultManager];
NSString *item;
NSArray *contents = [fManager contentsOfDirectoryAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] error:nil];

// >>> this section here adds all files with the chosen extension to an array
for (item in contents){
    if ([[item pathExtension] isEqualToString:extension]) {
        [matches addObject:item];
    }
}
return matches; }

上記の例は、かなり自明です。2番目の質問に答えてくれることを願っています。

于 2011-12-04T16:30:38.500 に答える
11

ディレクトリの内容を取得するには

- (NSArray *)ls {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSArray *directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: documentsDirectory];

    NSLog(@"%@", documentsDirectory);
    return directoryContent;
}

最後のパス コンポーネントを取得するには、

[[path pathComponents] lastObject]
于 2011-12-04T16:10:46.340 に答える
6

これは古い質問であることは承知していますが、良い質問であり、iOS ポスト サンドボックス化で状況が変わりました。

アプリ内の読み取り/書き込み可能なすべてのフォルダーへのパスにはハッシュが含まれるようになり、Apple はいつでもそのパスを変更する権利を留保します。確かに、アプリを起動するたびに変更されます。

必要なフォルダーへのパスを取得する必要があり、以前のようにハードコードすることはできません。

ドキュメント ディレクトリを要求すると、返される配列の位置は 0 になります。次に、その値を使用して NSFileManager に指定し、ディレクトリの内容を取得します。

以下のコードは iOS 7 および 8 で動作し、ドキュメント ディレクトリ内のコンテンツの配列を返します。自分の好みに合わせて並べ替えるとよいでしょう。

+ (NSArray *)dumpDocumentsDirectoryContents {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];

    NSError *error;
    NSArray *directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:&error];

    NSLog(@"%@", directoryContents);
    return directoryContents;
}
于 2015-04-16T18:07:54.373 に答える
4

より適切なファイル名の場合:

NSString *filename = [[url lastPathComponent] stringByAppendingPathExtension:[url pathExtension]];

于 2011-12-06T04:50:13.403 に答える
2
NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:dir_path];

NSString *filename;

while ((filename = [dirEnum nextObject]))
{
    // Do something amazing
}

ディレクトリ内のすべてのファイルを列挙するため

于 2015-09-13T16:12:35.397 に答える