YouTip LogoYouTip

Ios File

iOS File Handling | Novice Tutorial

iOS File Handling | Novice Tutorial

Novice Tutorial -- iOS Tutorial iOS TutorialiOS IntroductioniOS Environment SetupObjective C BasicsCreate First iPhone ApplicationiOS Actions and OutletsiOS – DelegatesiOS UI ElementsiOS AccelerometeriOS Universal ApplicationsiOS Camera ManagementiOS Location OperationsiOS SQLite DatabaseiOS Sending EmailiOS Audio & VideoiOS File HandlingiOS Map DevelopmentiOS In-App PurchaseiOS iAD IntegrationiOS GameKitiOS StoryboardsiOS Auto LayoutiOS Twitter and FacebookiOS Memory ManagementiOS Application Debugging iOS Audio & Video iOS Map Development In-depth Exploration Computer Science Programming Apple iOS Data Management Email and Instant Messaging Web Applications and Online Tools Software Smartphones

iOS File Handling


Introduction

File handling cannot be intuitively interpreted through the application. We can understand iOS file handling from the following examples.

File operations in iOS. Because the application is in a sandbox, file read and write permissions are limited, and files can only be read and written in a few directories.

Methods Used in File Handling

Below is a list of methods used for accessing and manipulating files.

In the following examples, you must replace the FilePath1, FilePath, and FilePath strings with complete file paths to achieve the desired operation.

Check if a file exists
 NSFileManager *fileManager = ; //Get documents directory NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectoryPath = [directoryPaths objectAtIndex:0]; if ([fileManager fileExistsAtPath:@""]==YES) { NSLog(@"File exists"); }
Compare the contents of two files
 if ([fileManager contentsEqualAtPath:@"FilePath1" andPath:@" FilePath2"]) { NSLog(@"Same content"); }
Check if a file is writable, readable, or executable
 if ([fileManager isWritableFileAtPath:@"FilePath"]) { NSLog(@"isWritable"); } if ([fileManager isReadableFileAtPath:@"FilePath"]) { NSLog(@"isReadable"); } if ( [fileManager isExecutableFileAtPath:@"FilePath"]){ NSLog(@"is Executable"); }
Move a file
 if([fileManager moveItemAtPath:@"FilePath1" toPath:@"FilePath2" error:NULL]){ NSLog(@"Moved successfully"); }
Copy a file
 if ([fileManager copyItemAtPath:@"FilePath1" toPath:@"FilePath2" error:NULL]) { NSLog(@"Copied successfully"); }
Delete a file
 if ([fileManager removeItemAtPath:@"FilePath" error:NULL]) { NSLog(@"Removed successfully"); }
Read a file
 NSData *data = [fileManager contentsAtPath:@"Path"];
Write to a file
 [fileManager createFileAtPath:@"" contents:data attributes:nil];
← Ios MapsIos Audio Video β†’