在iOS中查看自定义目录的内容

时间:2014-01-17 06:50:07

标签: ios iphone ipad ios7 xcode5

我正在我的应用程序中创建一个目录。有一个代码可以在xcode中查看目录的内容。例如,在android中,您可以使用文件管理器应用程序创建自定义目录并查看其内容。可以在苹果中完成类似的程序吗? 这是我用来创建目录的代码吗?

bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
    fileManager = [NSFileManager defaultManager];
    myImageDirectory = [fileManager URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask];
    if ([myImageDirectory count] == 1){
        NSLog(@"myImageDirectoryIs already present directory is already present");
    }else{
        directoryPath = [[myImageDirectory objectAtIndex:0] URLByAppendingPathComponent:bundleIdentifier];
        NSLog(@"myImageDirectory directory name = %@",[directoryPath absoluteString]);
        NSError *theError = nil;
        if (![fileManager createDirectoryAtURL:directoryPath withIntermediateDirectories:NO attributes:nil error:&theError]){
            NSLog(@"didnt write image data");
        }else{
           imagePath = [[directoryPath absoluteString] stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@_%@_%@_image.jpg",dIdNo,iIdNo,[self currentDateandTime]]];
           [imageData writeToFile:imagePath atomically:YES];
        }
    }

4 个答案:

答案 0 :(得分:1)

如果您的应用在模拟器中运行,则需要使用Finder。转到以下目录:

/Users/<username>/Library/Application Support/iPhone Simulator/<iOS version>/Applications/<uuid>/Library/Application Support

如果您的应用在设备上运行,则可以使用Xcode:

  • 连接设备
  • 选择菜单选项窗口 - &gt;组织者
  • 转到“设备”标签
  • 点击左侧设备菜单下的应用程序
  • 选择您的申请
  • 将列出目录内容,您可以选择下载所有内容。

答案 1 :(得分:0)

试试这个...... 创建目录

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/MyFolder"];

if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
    [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder

从目录中检索内容

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *customDirectory = [documentsDirectory stringByAppendingPathComponent:@"/MyFolder"];
NSError * error;
NSArray *directoryContents =  [[NSFileManager defaultManager]
                                       contentsOfDirectoryAtPath:customDirectory error:&error];
        for(NSString *strFile in directoryContents)
        {
            NSString *strVideoPath = [NSString stringWithFormat:@"%@/%@",customDirectory,strFile];

        if([[strVideoPath pathExtension] isEqualToString:@"mp4"] || [[strVideoPath pathExtension] isEqualToString:@"mov"])
        {

            [urlArray addObject:strVideoPath];
        }
    }

答案 2 :(得分:0)

您可以从以下代码中获取目录(MYNewFolder)的内容:

NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,   
 NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"MYNewFolder"];
NSArray *filePathsArray = [[NSFileManager defaultManager]
                           subpathsOfDirectoryAtPath:stringPath
                           error:nil];
for ( NSString *apath in filePathsArray )
{
    NSLog(@"inside the file path array=%d",apath);
}

答案 3 :(得分:0)

方式

-(NSArray *) getObjectsInDirectory:(NSString *)directory {
    NSFileManager * fm = [NSFileManager defaultManager];
    NSError * error = nil;

    NSArray * result = [fm contentsOfDirectoryAtPath:directory error:&error];

    return result;
}

如何对

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

此代码用于获取文档目录中的文件数组。

查看文件

NSLog(@"%@", files);

检查此课程,它可以让您在iOS中简化这么多事情: Atomic Class