Working With Files

Get File Size NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *URL = [documentsDirectory stringByAppendingPathComponent:@”SomeDirectoryName”]; URL = [URL stringByAppendingPathComponent:@”MyFileName.txt”]; NSError *AttributesError = nil; NSDictionary *FileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL error:&AttributesError]; NSNumber *FileSizeNumber = [FileAttributes objectForKey:NSFileSize]; long FileSize = [FileSizeNumber longValue]; NSLog(@”File: %@, Size: %ld”, URL, FileSize); Delete All Files In Directory […]

Read More

Disk Information

Getting Free Disk Space Based on the example at http://iphoneincubator.com/blog/device-information/how-to-obtain-total-and-available-disk-space-on-your-iphone-or-ipod-touch //***************************************** //***************************************** //********** GET FREE DISK SPACE ********** //***************************************** //***************************************** -(float)GetFreeDiskSpaceInBytes { float totalSpace = 0.0f; NSError *error = nil; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error]; if (dictionary) { NSNumber *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemFreeSize]; //Or […]

Read More

Where To Store Files

  Documents Directory Only documents and other data that is user-generated, or that cannot otherwise be recreated by your application, should be stored in the <Application_Home>/Documents directory and will be automatically backed up by iCloud. NSString *FilePath = [NSHomeDirectory() stringByAppendingPathComponent:@”Documents/users.xml”]; Caches Directory Data that can be downloaded again or regenerated should be stored in the […]

Read More

Create Unique Filename

NSString *filePath; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@”SomeDirectoryName”]; //Create unique filename CFUUIDRef newUniqueId = CFUUIDCreate(kCFAllocatorDefault); CFStringRef newUniqueIdString = CFUUIDCreateString(kCFAllocatorDefault, newUniqueId); path = [path stringByAppendingPathComponent:(NSString *)newUniqueIdString]; path = [path stringByAppendingPathExtension: @”MOV”]; CFRelease(newUniqueId); CFRelease(newUniqueIdString);

Read More

Using The Document Directory To Store Files

Show contents of Documents directory NSLog(@”Documents directory: %@”, [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]); Does File Exist NSString *path; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@”SomeDirectory”]; path = [path stringByAppendingPathComponent:@”SomeFileName”]; if ([[NSFileManager defaultManager] fileExistsAtPath:path]) { Delete File NSString *path; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@”SomeDirectory”]; path = [path stringByAppendingPathComponent:@”SomeFileName”]; […]

Read More

Read & Use Files

Display image from file UIImage *image1 = [[UIImage alloc] initWithContentsOfFile:filePath]; if (image1) { imageView.image = image1; [image1 release]; }

Read More

Output Text File Contents

Example converting a NSData file to a string for display NSString *s = [[[NSString alloc] initWithData:fileData encoding:NSUTF8StringEncoding] autorelease]; NSLog(@”File Contents = %@”, s);

Read More

Accessing files included in the application bundle

Note that application bundle files are read only. Getting path for a file in the application bundle //Get pointer to application bundle NSBundle *applicationBundle = [NSBundle mainBundle]; //Get path to a resource file in the bundle NSString *path = [applicationBundle pathForResource:@”myImageName” ofType:@”png”]; //Returns nil if not found

Read More