Getting A List of Contents

Latest update: July 2013

In this tutorial, we'll show you how to fetch a list of files from your FlashAir with an iOS App. We'll use command.cgi to do this.

We will create an application that retrieves information about files in a specified directory of the FlashAir and displays the results on the screen.

We will use StoryBoards in this tutorial, but here is not explained how to use StoryBoards. If you will use it for the first time, try to develop using examples from the apple developer website and the actual Sample Code.

Creating the Screen Layout

Here is the screen layout of the app we want to create:

image shows the screen layout

We will set the app so that if the "Get List" button is tapped, the number of files in the directory will appear in the "labelCount" box and the file list will display in the "textViewList" box.

To do this, we will add the following as class property objects:

  • Round Rect Button ( UIButton)
    • Get List : Gets the content list
  • Label ( UILabel)
    • labelCount : Show results after fetching the number of files
  • Text View ( UITextView)
    • textViewList : show the results after fetching the file list

We will also declare a function in the FSViewController class that will do some action when it detects the UIButton has been pressed. (We will write the implementation for this function later in this tutorial.)

The layout elements will be declared in FSViewController.h. The modified file will look like this:


#import <UIKit/UIKit.h>

@interface FSViewController : UIViewController

@property (strong, nonatomic) IBOutlet UILabel *labelCount;
@property (strong, nonatomic) IBOutlet UITextView *textViewList;
- (IBAction)buttonPush:(id)sender;


Writing the Methods

Getting the File List

To get the file list, we will use command.cgi with op=100. Since we need to specify the folder from which we wish to fetch data, we will supply the DCIM folder.

In order to execute this cgi command, we will use NSString stringWithContentsOfURL . This NSString function will allow us to get the information at the specified URL in UTF-8 encoding and will return any error data in an NSError object.

FSViewController.m [Part 1]

#import "FSViewController.h"

@interface FSViewController ()

@implementation FSViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.labelCount.text = @"";
    self.textViewList.text = @"";

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

- (IBAction)buttonPush:(id)sender {
    NSError *error = nil;
    self.labelCount.text = @"";
    self.textViewList.text = @"";
    // Get file list
    // Make url
    NSURL *url100 = 
        [NSURL URLWithString:@"http://flashair/command.cgi?op=100&DIR=/DCIM"];
    // Run cgi
    NSString *dirStr = 
        [NSString stringWithContentsOfURL:url100 encoding:NSUTF8StringEncoding error:&error];
    if ([error.domain isEqualToString:NSCocoaErrorDomain]){
        NSLog(@"error100 %@\n",error);
    // Display results
    self.textViewList.text = dirStr;
  • Lines 26-27:
    Sets the URL that you will use to call the command. We use op=100&DIR=/DCIM.
  • Lines 29-30:
    We want to make sure that the characters returned are encoded in UTF-8. We will specify this using encoding:NSUTF8StringEncoding
  • Line 36:
    All the file information is returned to textViewList.

Getting the Number of Files

To get the number of files, we use command.cgi with op=101. Just as when we retrieved the file list, when getting the number of files, we will need to specify a directory. We will specify DCIM as the target directory again.

FSViewController.m [Part 2]

    // Make url
    NSURL *url101 = 
        [NSURL URLWithString:@"http://flashair/command.cgi?op=101&DIR=/DCIM"];
    // Run cgi
    NSString *cntStr = 
        [NSString stringWithContentsOfURL:url101 encoding:NSUTF8StringEncoding error:&error];
    if ([error.domain isEqualToString:NSCocoaErrorDomain]) {
        NSLog(@"error101 %@\n",error);
    // Display results
    self.labelCount.text =[@"Count=" stringByAppendingString:cntStr];
  • Lines 2-3:
    We will use op=101 and DIR=/DCIM as parameters in our URL.
  • Line 12:
    We set the return value (number of files) to be stored in LabelCount.


Once the program is complete, we will check to see if it works. When you tap the "Get List" button, the number of files and list of the directory contents should be displayed.

show content list

You have now completed the tutorial on getting a content list.

Sample Code (21KB)

All sample code on this page is licensed under BSD 2-Clause License