IPhone AppObtainFileThe tutorial is the content to be introduced in this article.IPhoneGetApp, It can only access some files in its root directory called sandbox ).AppPublishIPhoneThe directory structure is as follows:
1. The app root can be accessed using NSHomeDirectory;
2. The Documents directory is the place where we can write and save the file:
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentsDirectory = [paths objectAtIndex:0];
.
3. In the tmp directory, we can write data that is needed when the program is running. The data written in the tmp directory will not exist after the program exits. You can use
- NSString *NSTemporaryDirectory(void);
Method;
4. file operations can be performed through NSFileManage, And the instance can be obtained through [NSFileManger defaultManger.
Related Operations:
Create a directory: for example, to create a test directory under Documents,
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
-
- NSString *documentsDirectory = [paths objectAtIndex:0];
-
- NSLog(@”%@”,documentsDirectory);NSFileManager *fileManage = [NSFileManager defaultManager];
- NSString *myDirectory = [documentsDirectory stringByAppendingPathComponent:@“test”];
- BOOL ok = [fileManage createDirectoryAtPath:myDirectory attributes:nil];
Obtain all file names in a directory: The above myDirectory) is available.
- NSArray * file = [fileManager subpathsOfDirectoryAtPath: myDirectory error: nil];
- Or
- NSArray * files = [fileManager subpathsAtPath: myDirectory];
Read an object:
- NSData *data = [fileManger
- contentsAtPath:myFilePath];//myFilePath
Is the name of the file containing the complete path
Or directly use the NSData class method:
- NSData *data = [NSData dataWithContentOfPath:myFilePath];
Save an object:
You can use NSFileManager's
- - (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary *)attr;
Or NSData
- - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
- - (BOOL)writeToFile:(NSString *)path options:(NSUInteger)writeOptionsMask error:(NSError **)errorPtr;
Summary:IPhone AppObtainFileI hope this article will be helpful to you.