Files A few quick points about files under iOS:
The sandbox typically includes the application bundle, a folder called "Documents", a folder called "Library", a folder called "tmp" and perhaps other folders. See "File System Basics" in the programming guide for details on these folders. The application bundle is a pseudo-folder that holds everything that makes up the application, code, resources, info.plist, etc. Accessing the file: Files are identified by a path name or an NSURL object. NSURL can also refer to resources on the net. See URL Loading System Programming Guide for the net capabilities. There are several ways to construct them. This creates a reference to a file in the application bundle: NSURL* url = [[NSBundle mainBundle] URLForResource:@"MyImage" With some types you can reach resources in the bundle without using NSURL UIImage* theImage = [UIImage imageNamed:@"testImage.png"]; For files in the user's sandbox there are standard names for well known directories like "Documents". Reading and writing a file A file was originally a sequence of bytes of an arbitrary length with a beginning and an end. That suited the storage media of the time, usually decks of cards or reels of tape. Today, a file might be structured in a complex way. Low level -- the file is a sequence of bytes. Using UNIX calls you can read and write bytes anywhere in the file. The meaning of the bytes is up to you. Mid level -- the file contains objects in the Objective-C sense. You can read and write NSData, NSStrings, NSNumbers, NSDictionarys, etc. String Programming Guide tells how to read and write string objects. In general, objects can be written and read with files, even complex ones. However, they must be "flattened" to be stored in a file. The official term is "serialization", and it is covered in the Archives and Serialization Programming Guide. Briefly, any class that intends to serialize its objects must adopt and implement the NSCoding protocol. That permits it to determine how to serialize each of its components. Sample code is in this project. High level -- using Core Data the file becomes an automatic persistent backup for chosen objects. It's not a data base, but it can become the bridge to a DB. File Sharing An app can share files through iTunes. See the discussion of UIFileSharingEnabled in Information Property List Key Reference. It can "receive" documents of chosen types handed from other apps through Document Interaction. The page was last updated
Wednesday, May 16, 2012 7:16 PM
|