APP sandbox to get files

Source: Internet
Author: User

For an app running on the iPhone, it can only access some files (so-called sandbox) in its root directory ).
After an app is published to the iPhone, its 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 files. Generally, we can use:
* 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, if you want 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];Get all file names in a directory:
(Above mydirectory) 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 file name that contains 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;

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: 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.