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 […]
Category: File Saving and Loading
Sharing Files With PC’s and MAC’s
Good resources http://mobiforge.com/developing/story/importing-exporting-documents-ios
Sharing Files Between Apps
Good resources http://mobiforge.com/developing/story/importing-exporting-documents-ios
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 […]
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 […]
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);
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 & Use Files
Display image from file UIImage *image1 = [[UIImage alloc] initWithContentsOfFile:filePath]; if (image1) { imageView.image = image1; [image1 release]; }
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);
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