【问题标题】:How to list files of a folder using contentsOfDirectoryAtURL如何使用 contentsOfDirectoryAtURL 列出文件夹的文件
【发布时间】:2017-10-29 01:27:07
【问题描述】:

我解释了我的问题:我的计算机中有一些文件,我想通过我的 iPhone 读取它们,这要归功于 Wi-fi(Wi-fi 开始,然后是 3G 网络)。

  • 我在我的计算机上打开了 WebServer(也许它不是解决我的问题的更好解决方案,我不知道),所以我成功地读取了具有如下确切路径的文件: NSURL *serverFile = [NSURL URLWithString:@"http://192.168.1.9/AccountNumber.txt"]; NSString *number = [NSString stringWithContentsOfURL:serverFile encoding:NSUTF8StringEncoding error:nil]; NSLog(@"%@",number); // It works

  • 为了检查一个文件是否存在,我在这里使用 NSURLSession:

    NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:dossierDevice]; request.HTTPMethod = @"HEAD"; NSURLSession* session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration ephemeralSessionConfiguration]]; NSURLSessionDataTask* task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){ NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode]; if (statusCode == 200 || statusCode == 403) { NSLog(@"EXIST");// success } else { NSLog(@"DON'T EXIST");// failure } }]; [task resume];

  • 但现在我的问题是列出文件夹的文件。之前,当我从我的电脑对自己(使用模拟器)进行测试时,我使用了contentsOfDirectoryAtPath,它工作得很好。 但是现在,当我使用像 NSMutableArray *allAccounts = [[NSMutableArray alloc] initWithArray:[[NSFileManager defaultManager] contentsOfDirectoryAtURL:AccountFolder includingPropertiesForKeys:nil options:NSDirectoryEnumerationSkipsHiddenFiles error:nil]]; 这样的东西时,它会返回一个空的 NSMutableArray。

我该如何解决这个问题?谢谢。

PS:我在枚举文件夹中的文件列表时遇到同样的问题

【问题讨论】:

    标签: ios webserver nsurl nsfilemanager enumerator


    【解决方案1】:

    这种枚举只适用于带有 file:/// 而不是 http:// 的 NSURL。您可以使用文件服务器(例如 webdav、afp、smb 或类似的)而不是网络服务器。

    【讨论】:

      猜你喜欢
      • 2022-07-06
      • 2012-07-18
      • 2023-03-27
      • 1970-01-01
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多