IPhone app file operations

Source: Internet
Author: User

IPhone AppObtainFileThe tutorial is the content to be introduced in this article.IPhoneGetApp, It can onlyAccessRootDirectorySome files (so-called sandbox). OneAppPublishIPhoneIt gets the DirectoryStructureAs follows:

1. Get the appRootNS can be usedHomeDirectory () Access;

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. We can write someProgramRequired for runningDataThe data written in it will not exist after the program exits. You can use

    1. Nsstring * NSTEmPorarydirectory (void );

MethodGet;

4. Main filesOperationYou can use NSFileManage. You can use [nsfilemanger DEFAUlTmanger] Get itInstance.

Related Operations:

CreateA directory: for example, to create a test directory under documents,

    1. nsarray * paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes);
    2. nsstring * documentsdirectory = [paths objectatindex: 0];
    3. nslog (@ "% @", documentsdirectory ); nsfile Manager * filemanage = [nsfilemanager default Manager];
    4. nsstring * mydirectory = [documentsdirectory string byappendingpathcomponent: @ "test"];
    5. bool OK = [filemanage Create directoryatpath: mydirectory attributes: nil];

Get all file names in a directory: (The above mydirectory) 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;

The program contains three folders: Documents, library, and TMP.

1. Documents:

This directory is used to store data in the program, except the Setting data based on nsuserdefaults.

Obtain the program's documentsCodeYes:

 
Nsarray * paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes); nsstring * documentsdirectory = [paths objectatindex: 0];

 

The address of the documents directory is stored in documentsdirectory.

2. Library:

This directory contains two subdirectories: caches and preferences.

Preferences stores the settings Data Based on nsuserdefaults. For example, you can see a plist file, which is the configuration item we set last time and is read and written by the settings program. Open it and we can see that the last setting we made was saved in this file:

2.1 The code for getting the library directory address is similar to those of documents:

 
Nsarray * paths = nssearchpathfordirectoriesindomains (nslibrarydirectory, nsuserdomainmask, yes); nsstring * librarydirectory = [paths objectatindex: 0];

 

2.2 obtain the caches directory address:

 
Nsarray * paths = nssearchpathfordirectoriesindomains (nscachesdirectory, nsuserdomainmask, yes); nsstring * cachesdirectory = [paths objectatindex: 0];

 

3. tmp:

We can store temporary files in this directory. when the device is connected to iTunes, data in TMP will be synchronized. Our program should be responsible for deleting the content under the tmp directory.

Get the tmp directory:

The methods for obtaining the tmp directory written on many documents are as follows:

 
Nsstring * temppath = nstemporarydirectory ();

 

But what I found was not the tmp directory of the program,:

 
/Var/folders/TP/pl9mdxtx199bs8_k29n89ff80000gn/T/

 

It seems that there is no such parameter as nslibrarydirectory that can be directly used to obtain this directory.

However, if we can get the root directory of this program, we can get the tmp directory:

 
// Obtain the program's root directory nsstring * homedirectory = nshomedirectory (); // obtain the tmp directory address nsstring * tmpdirectory = [homedirectory stringbyappendingpathcomponent: @ "tmp"];

 

Tmpdirectory stores the tmp directory of the program.

I can use filemanager to create sub-directories and files in it, but I have a program that tries to download files to it using FTP, but a file write error occurs.

After obtaining the preceding directory address, you only need:

 
Nsstring * filename = [documentsdirectory stringbyappendingpathcomponent: @ "filename.txt"];

on a real machine, nstemporarydirectory corresponds to the tmp directory of the app directory.

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.