Chapter 13. File Handling

WHAT YOU WILL LEARN IN THIS CHAPTER

  • Where your applications are stored on the iPhone

  • The various folders within your Applications folder

  • How to read and write to files in the Documents and tmp folders

  • How to use a property to store structured data

  • How to programmatically retrieve values stored in a property list

  • How to modify the values retrieved from a property list and save the changes to a file

All the applications you have developed up to this point are pretty straightforward — the application starts, performs something interesting, and ends. In Chapter 11, you saw how you can make use of the application settings feature to save the preferences of your application to a central location managed by the Settings application. Sometimes, however, you simply need to save some data to your application's folder for use later. For example, rather than keep files you download from a remote server in memory, a better (and more effective and memory efficient) method might be to save them in a file so that you can use them later (maybe even after the application has shut down and restarted).

In this chapter, you learn more about how you can persist data in your application so that you can use it later, even after the application has restarted. You learn the two available approaches: saving the data as files and as a property list.

UNDERSTANDING THE APPLICATION FOLDERS

So far, you have been busy deploying your applications onto the iPhone Simulator and have not spent much time exploring where the applications get stored in the iPhone file system. This section helps you understand the folder structure of the iPhone.

On the desktop, the content of the iPhone Simulator is stored in the ~/Library/Application Support/iPhone Simulator/User/ folder.

Note

The ~ (tilde) represents the current user's directory. Specically, the preceding directory is equivalent to:

/Users/<username>/Library/Application Support/iPhone Simulator/User/.

Within this folder are five Subfolders:

  • Applications

  • Library

  • Media

  • Root

  • tmp

The Applications folder is the folder that contains all your installed applications (see Figure 13-1). Within the Applications folder are several folders with long filenames. These filenames are generated by Xcode to uniquely identify each of your applications. Within each application's folder, you can find your application's executable file (the .app file, which includes all embedded resources), together with a few other folders, such as Documents, Library, and tmp. On the iPhone, all applications run within their own sandboxed environment — that is, an application can access only the files stored within its own folder; it cannot access the folders of other applications.

Figure 13-1

Figure 13.1. Figure 13-1

Using the Documents and Library Folders

The Documents folder is where you can store files used by your application, whereas the Library folder stores the application-specific settings. The tmp folder stores temporary data required by your application.

So how you do write to these folders? See the following Try It Out for an example of doing just that. You need to download the indicated code files to work through the project.

Storing Files in the Temporary Folder

In addition to storing files in the Documents directory, you can store temporary files in the tmp folder. Files stored in the tmp folder are not backed up by iTunes, so you need to find a permanent place for the files you want to be sure to keep. To get the path to the tmp folder, you can call the NSTemporaryDirectory() function, like this:

-(NSString *) tempPath{
    return NSTemporaryDirectory();
}

On a real device, the path returned for the tmp folder would be: /private/var/mobile/Applications/<application_id>/tmp/.

However, on the iPhone Simulator, the path returned is actually /var/folders/<application_id>/-Tmp-/data.txt, not ~/Library/Application Support/iPhone Simulator/User/Applications/<application_id>/tmp/.

The following statement returns the path of a file to be stored in the tmp folder:

NSString *fileName = [[self tempPath]
  stringByAppendingPathComponent:@"data.txt"];

USING PROPERTY LISTS

In iPhone programming, you can use property lists to store structured data using key/value pairs. Property lists are stored as XML files and are highly transportable across file systems and networks. For example, you might want to store a list of AppStore applications titles in your application. Because applications in the AppStore are organized into category, it would be natural to store this information using a property list employing the structure shown in Figure 13-3.

Figure 13-3

Figure 13.3. Figure 13-3

In Xcode, you can create and add a property list in the Resources folder of your application and populate it with items using the built-in Property List Editor. When the application is deployed, the property list is deployed together with the application. Programmatically, you can retrieve the values stored in a property list using the NSDictionary class. More important, if you need to make changes to a property list, you can write the changes to a file so that subsequently you can refer to the file directly instead of the property list.

In the following Try It Out, you will create a property list and populate it with some values. You will then read the values from the property list during runtime, make some changes, and then save the modified values to another property list file.

Note

If you want to store application-specific settings that the user can modify outside your application, you should consider using the NSUserDefaults class to store the settings in the Settings application. Application Settings are discussed in Chapter 11.

SUMMARY

This chapter demonstrated how to write a file to the file system of the iPhone and how to read it back. In addition, you saw how structured data can be represented using a property list and how you can programmatically work with a property list using a dictionary object. In the next chapter, you will see how you can make use of databases to store more complex data.

EXERCISES

  1. Describe the uses of the various folders within an application's folder.

  2. What is the difference between the NSDictionary and NSMutableDictionary classes?

  3. Name the paths of the Documents and tmp folders on a real device.

  • WHAT YOU HAVE LEARNED IN THIS CHAPTER

TOPIC

KEY CONCEPTS

Subdirectories in each of the applications folder

Documents, Library, and tmp

Getting the path of the Documents directory

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

Getting the path of the tmp directory

-(NSString *) tempPath {
    return NSTemporaryDirectory();
}

Check whether file exists

if ([[NSFileManager defaultManager]
fileExistsAtPath:filePath]) {
}

Location of the Documents directory on a real device

/private/var/mobile/Applications/<application_id>/Documents/

Location of the tmp directory on a real device

/private/var/mobile/Applications/<application_id>/tmp/

Load a property list from the Resources folder

NSString *pListPath = [[NSBundle mainBundle]
                          pathForResource:@"Apps"
                          ofType:@"plist"];

Create a mutable copy of an NSDictionary object

NSDictionary *dict = [[NSDictionary alloc]
                         initWithContentsOfFile:pListPath];
NSMutableDictionary *copyOfDict = [dict mutableCopy];
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.56.28