IOS-about SandBox mechanism and file read/write Methods

Source: Internet
Author: User

IOS-about SandBox mechanism and file read/write Methods

SandBox (SandBox) in iOS is a security system, which stipulates that the APP can only read files in folders created for the APP, but cannot access content in other places. All non-code files are stored in this place, such as slice, sound, attribute list, and text files. That is:

Each application cannot access the content in the SandBox of another application across its own SandBox without permission authentication.

By default, each APP's SandBox contains three folders: Documents, Library, and tmp. This APP can only perform file read/write operations under these three directories.
Obtain the SandBox path:

let home: String = NSHomeDirectory()
Documents: Apple recommends that you save the file data created or browsed in the APP here, and iTunes will back up the file data. library: stores the default settings or other status information of the APP, which is backed up by iTunes. library/Preferences: Preferences file of the APP. You can use NSUserDefaults to obtain and set the Preferences. library/Cached: stores Cached files. files in this directory will not be deleted when the APP exits. however, iTunes will not back up this directory. tmp: the place where temporary files are created.

You can obtain the paths as follows:

// Use NSSearchPathForDirectoriesInDomains to obtain the full path let paths: Array = NSSearchPathForDirectoriesInDomains (NSSearchPathDirectory. DocumentDirectory, NSSearchPathDomainMask. UserDomainMask, true) as Array! Print (paths: (paths) let documentsPath: String = paths [0] as String! Print (documents: (documentsPath) let library: String = NSSearchPathForDirectoriesInDomains (. libraryDirectory ,. userDomainMask, true) [0] print (library: (library) let preferencs: String = NSSearchPathForDirectoriesInDomains (. preferencePanesDirectory ,. userDomainMask, true) [0] print (preferencs: (preferencs) let caches: String = NSSearchPathForDirectoriesInDomains (. cachesDirectory ,. userDomainMask, true) [0] print (caches: (caches ))
NSBundle

NSBundle contains all the files packaged by the APP. For example, the Info. plist file contains the basic information of the APP.

// Get the Info. plist file let infoPath: String = NSBundle. mainBundle (). pathForResource (Info, ofType: plist) as String! // Read the contents in the Dictionary format. infos = NSMutableDictionary (contentsOfFile: infoPath) as NSMutableDictionary! // Get CFBundleName information let bundleName: String = infos [CFBundleName]! Stringprint (bundleName)

The method for writing content to files in NSBundle is the same as that of other files. See below.
Generally, you can use NSBundle to load the xib file:

var nibs: [AnyObject] = NSBundle.mainBundle().loadNibNamed(testNib, owner: nil, options: nil)var testNib: UIView = nibs.last as! UIView
File read/write

File read/write is a very basic operation. In iOS, similar methods such as contentsOfFile and writeToFile are commonly used.
First, obtain the file path:

let docPath: String = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0]let filePath: String = docPath.stringByAppendingString(/filename)

Find the filename file in the Documents directory of SandBox. If no file exists, create a new file.

Read files

Read String:

var content = String(contentsOfFile: filePath, encoding: NSUTF8StringEncoding)var content = String(data: NSData(contentsOfFile: filePath)!, encoding: NSUTF8StringEncoding)!

Read NSArray, NSDictionary, NSData:

var array = NSArray(contentsOfFile: filePath)var dict = NSDictionary(contentsOfFile: filePath)var data = NSData(contentsOfFile: filePath)

Read image:

Let imagePath: String = NSBundle. mainBundle (). pathForResource (Image, ofType: jpg) as String! Var image: UIImage = UIImage (contentsOfFile: imagePath )! // Read var data: NSData = NSData (contentsOfFile: imagePath) through NSData )! Image = UIImage (data: data )!

Read network images:

var url: NSURL = NSURL(string: http://imgphoto.gmw.cn/attachement/jpg/site2/20151010/eca86bd9dc471782b9ff28.jpg)!data = NSData(contentsOfURL: url)!image = UIImage(data: data)! 
Write files

Write String:

Try testing content again. writeToFile (filePath, atomically: true, encoding: NSUTF8StringEncoding) // write Stringvar data = NSMutableData () data through NSData. appendData (testing content. dataUsingEncoding (NSUTF8StringEncoding, allowLossyConversion: true )!) Data. writeToFile (filePath, atomically: true)

Write NSArray, NSDictionary, NSData:

array.writeToFile(filePath, atomically: true)dict.writeToFile(filePath, atomically: true)data.writeToFile(filePath, atomically: true)
Store images

The format of NSData is usually used to write images to files.

Let docPath: String = NSSearchPathForDirectoriesInDomains (NSSearchPathDirectory. documentDirectory, NSSearchPathDomainMask. userDomainMask, true) [0] imagePath = docPath. stringByAppendingString (/testImage.jpg) data. writeToFile (imagePath, atomically: true) image = UIImage (contentsOfFile: imagePath )! Data = NSData (data: UIImagePNGRepresentation (image )!) // UIImageJPEGRepresentation (image, 1.0) // JPEG compression is more var imagePath2: String = docPath than PNG compression. stringByAppendingString (/testImage2.png) data. writeToFile (imagePath2, atomically: true) image = UIImage (contentsOfFile: imagePath2 )!

 

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.