【问题标题】:View the contents of Custom Directory in iOS在 iOS 中查看自定义目录的内容
【发布时间】:2014-01-17 06:50:07
【问题描述】:

我正在我的应用程序中创建一个目录。是否有代码可以在 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];
        }
    }

【问题讨论】:

    标签: ios iphone ipad ios7 xcode5


    【解决方案1】:

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

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

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

    • 连接设备
    • 选择菜单选项 Window -> Organizer
    • 转到“设备”选项卡
    • 点击左侧设备菜单下的应用程序
    • 选择您的应用程序
    • 将列出目录内容,您可以选择下载所有内容。

    【讨论】:

      【解决方案2】:

      试试这个... 创建目录

      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];
              }
          }
      

      【讨论】:

        【解决方案3】:

        您可以从下面的代码中获取目录(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);
        }
        

        【讨论】:

          【解决方案4】:

          方法

          -(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

          【讨论】:

            猜你喜欢
            • 2016-05-05
            • 2018-04-13
            • 1970-01-01
            • 2018-04-19
            • 2010-12-08
            • 2011-12-17
            • 2013-08-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多