iOS Development-ui Learning-sandbox mechanism & file operations

Source: Internet
Author: User

? Apple provides a sandbox mechanism for running software each sandbox contains 3 folders: Documents, Library, and TMP. Because of the sandbox mechanism applied, the application can only read and write files in several directories. Documents: Apple recommends that file data that is created in the program or browsed in the program be saved in this directory, which is included in itunes backup and restore? Library: The default settings or other status information of the stored program;?   Library/caches: Store cache files, itunes does not back up this directory, files in this directory will not be deleted in the app exit; TMP: Provides a place to create temporary files instantly, these three folders There are several ways to obtain its path for documents and libraries, and TMP has only one way. Most of the ways to get paths are file manipulation functions in the C language.
1 //Get and print the app's sandbox with three folders, Doucment,library,tmp2NSString * HomePath = [NSString stringWithFormat:@"%@", Nshomedirectory ()];3NSLog (@"%@", HomePath);4     5     6 //the first way to get and print the document path7NSString *documentpath = [NSString stringWithFormat:@"%@/documents", Nshomedirectory ()];8NSLog (@"%@", documentpath);9     Ten      One //The second way to get and print the document path is to get an array, and the document path is the first element of the array.  ANsarray *documentpath2 =nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); -NSLog (@"%@", [DocumentPath2 Objectatindex:0]); -      the      -      - //Method 1 for obtaining and printing the caches path: -Nsarray *cachepath =nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES); +NSLog (@"%@", cachepath[0]); -      +      A //Method 2 to obtain and print the cache path: atNSString *cachepath2 = [NSString stringWithFormat:@"%@/library/caches", Nshomedirectory ()]; -NSLog (@"%@", cachePath2); -      -      - //methods for obtaining and printing the TMP path -NSString *tmppath =nstemporarydirectory (); inNSLog (@"%@", Tmppath);

Create a new folder in the program folder operation;
1 //Create a folder under document2 //Create a path First3NSString *newpath = [NSString stringWithFormat:@"%@/documents/new", Nshomedirectory ()];4NSLog (@"%@", NewPath);5 //Use Nsfilemanager to create a folder using the previously created path, Yesorno returns whether a successful message was created6BOOL Yesorno = [[Nsfilemanager defaultmanager]createdirectoryatpath:newpath Withintermediatedirectories:yes Attributes:nil Error:nil];

After the new is completed, there is one more folder under documents new.

Create file New.mp3:

1 //     Create file 2//     First define the file path and filename good 3     nsstring *newfile = [NSString stringWithFormat:@ "%@/new.mp3", NewPath]; 4 //     Creating a file using Createfileatpath 5     [[Nsfilemanager defaultmanager]createfileatpath:newfile Contents:nil Attributes:nil];

deleting files New.mp3

1 //     Delete file 2    [[Nsfilemanager defaultmanager]removeitematpath:newfile Error:nil]; 3     

iOS Development-ui Learning-sandbox mechanism & file operations

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.