IPhone App file acquisition tutorial

Source: Internet
Author: User

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:

 
 
  1. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  2. 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

 
 
  1. 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,

 
 
  1. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  2.  
  3. NSString *documentsDirectory = [paths objectAtIndex:0];  
  4.  
  5. NSLog(@”%@”,documentsDirectory);NSFileManager *fileManage = [NSFileManager defaultManager];  
  6. NSString *myDirectory = [documentsDirectory stringByAppendingPathComponent:@“test”];  
  7. BOOL ok = [fileManage createDirectoryAtPath:myDirectory attributes:nil]; 

Obtain all file names in a directory: The above myDirectory) is available.

 
 
  1. NSArray * file = [fileManager subpathsOfDirectoryAtPath: myDirectory error: nil];
  2. Or
  3. NSArray * files = [fileManager subpathsAtPath: myDirectory];

Read an object:

 
 
  1. NSData *data = [fileManger   
  2. contentsAtPath:myFilePath];//myFilePath 

Is the name of the file containing the complete path

Or directly use the NSData class method:

 
 
  1. NSData *data = [NSData dataWithContentOfPath:myFilePath]; 

Save an object:

You can use NSFileManager's

 
 
  1. - (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary *)attr; 

Or NSData

 
 
  1. - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;  
  2. - (BOOL)writeToFile:(NSString *)path options:(NSUInteger)writeOptionsMask error:(NSError **)errorPtr;  

Summary:IPhone AppObtainFileI hope this article will be helpful to you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.