---sandbox for iOS development

Source: Internet
Author: User
Tags vars

iOS Sandbox directory parsing

For security reasons, the sandbox mechanism of the iOS system stipulates that each app can access only the files under the current sandbox directory (with exceptions, such as the ability of the system contacts to be accessed by third-party apps under user authorization), a rule that reveals the closeness of the iOS system. First, several major directories in the sandbox there are similar directory structures under each sandbox, as shown (from Apple's official documentation): Each app's sandbox directory is similar, mainly containing the 4 directories shown in the figure: 1, Myapp.app① Store Content The directory contains data for the application itself, including resource files and executables. After the program starts, the code or resources are dynamically loaded into memory from the directory as needed, and lazy loading is used here. ② the entire directory is read-only in order to prevent tampering, the application will sign the directory when it is installed. In the case of non-jailbreak, the contents of this directory cannot be changed, and if the contents of the directory are changed on the jailbreak device, the corresponding signature will be changed, in which case the result of the Apple website description is that the application will not start and I have not practiced it. Whether ③ will be synced by itunes No 2, Documents① store content We can save the application's data file in that directory. However, these data types are limited to non-renewable data, and renewable data files should be stored in the Library/cache directory. Whether ② will be synced by itunes is 3, Documents/inbox① store content This directory is used to save files opened by external apps requesting the current application. For example, our application is called a, several open file formats are registered to the system, B is applied with a file F in a supported format, and application call a opens F. Since F is currently in the sandbox of the B application, we know that the sandbox mechanism is not allowed A to access the files in the B sandbox, so Apple's solution is to speak F copy a copy of the Documents/inbox directory to the A application, and then let a open f. ② if itunes sync is 4, Library① store content Apple recommends storing the default settings or other status information. ② will be synced with itunes Yes, but in addition to the Caches subdirectory 5, Library/caches① storage content is mainly cached files, the user in the process of the cache can be saved in this directory. As mentioned earlier, the documents directory is used to store non-renewable files, so this directory is used to hold those renewable files, such as data requested by the network. For this reason, the application usually also needs to be responsible for deleting these files. Whether the ② will be synced by itunes No. 6, Library/preferences① Store the content application preferences file. The settings data we write with Nsuserdefaults will be saved to a plist file in that directory, which is called Write Plist! ② isNo will be synced by itunes is 7, Tmp① store the contents of various temporary files, save the app when the file is not needed to start again. Moreover, when the application no longer needs these files should be actively deleted, because the contents of the directory can be removed from the system at any time, the current known one possible reason is that the system disk storage space is insufficient. Whether ② will be synced by itunes no   ii. How to get the main directory path 1, sandbox directory [OBJC]View Plaincopy
    1. NSLog (@ "%@", Nshomedirectory ());
Output Result: [OBJC]View Plaincopy < param name= "wmode" value= "Transparent" >
    1. /var/mobile/applications/326640A7-6E27-4C63-ba5e- 7391F203659A
2. tmp [OBJC]View Plaincopy
    1. NSLog (@ "%@", Nstemporarydirectory ());
Output Result: [OBJC]View Plaincopy < param name= "wmode" value= "Transparent" >
    1. /private/var/mobile/applications/326640A7-6E27-4C6 3-ba5e-7391F203659a/tmp/
3, Myapp.app [OBJC]View Plaincopy
    1. NSLog (@ "%@", [[NSBundle Mainbundle] Bundlepath]);
Output Result:
[OBJC]View Plaincopy
    1. /var/mobile/applications/326640A7-6E27-4C63-ba5e- 7391F203659a/phonecall. App
4. Documents [OBJC]View Plaincopy
    1. Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
    2. NSString *path = [Paths Objectatindex:0];
    3. NSLog (@ "%@", Path);
Output Result: [OBJC]View Plaincopy < param name= "wmode" value= "Transparent" >
    1. /var/mobile/applications/32664 0a7-6e 27-4c6 3-ba5e-73 91f20< Span class= "Xcodenumber" >3659A/ documents  
Here the Nssearchpathfordirectoriesindomains method needs to be explained, and its declaration is as follows: [OBJC]View Plaincopy
    1. Foundation_export nsarray *nssearchpathfordirectoriesindomains (nssearchpathdirectory directory,  Nssearchpathdomainmask Domainmask, BOOL Expandtilde);
The method is used to return a collection of paths for the specified name in the specified range of directories. There are three parameters:
    • Directory
The enum value of the Nssearchpathdirectory type, indicating the name of the directory we are searching for, such as here with nsdocumentdirectory that we are searching for the documents directory. If we change it to nscachesdirectory it means that we are searching for the Library/caches directory.
    • Domainmask
An enum value of type Nssearchpathdomainmask that specifies the search scope, where nsuserdomainmask indicates that the scope of the search is limited to the currently applied sandbox directory. It can also be written as Nslocaldomainmask (representing/library), Nsnetworkdomainmask (for/network), etc.
    • Expandtilde
A bool value that indicates whether wavy lines are expanded ~. We know that in iOS the full-write form is/user/username, the value of Yes is written as a full-written form, for No is directly written as "~". Third, reference Document 1, Apple Official document https://developer.apple.com/library/mac/documentation/FileManagement/Conceptual/ Filesystemprogrammingguide/introduction/introduction.html#//apple_ref/doc/uid/tp40010672-ch1-sw1

---sandbox for iOS development

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.