iOS path sandbox file management, there is a need for friends to refer to below.
First, the sandbox mechanism in iOS
iOS applications can only read files to the file system that they create, a separate, closed, and secure space called a sandbox. It typically holds package files (executables), pictures, audio, video, plist files, SQLite databases, and other files.
Each application has its own separate storage space (sandbox)
Generally, applications are not accessible to each other
Location of the simulator sandbox
/user/username/library/application Support/iphone Simulator
When we created the application, there were three files in each sandbox, namely document, library, and temp.
Document: Typically, persistent data is placed in this directory and can be added to a subfolder, which is included in itunes Backup and restore.
Library: Set default settings and other status information for programs
Temp: Creates a directory of temporary files that are automatically erased when the iOS device restarts
Get the Sandbox catalog
NSString *homepath = Nshomedirectory ()
Nsarray *paths = Nssearchpathdordirectoriesindomains (Nsdocumentdicrectory,, Nsuserdomainmark, YES); NSString *docpath = [Paths lastobject];
Nsarray *paths = Nssearchpathfordirectoriseindomains (Nslibrarydirectory, Nsuserdomainmask, YES); NSString *docpath = [Paths lastobject];
Nsarray *paths = Nssearchpathfordirectoriseindomains (Nscachesdirectory, Nsuserdomainmask, YES); NSString *docpath = [Paths lastobject];
NSString *temp = Nstemporarydirectory ();
Second, the method of processing the NSString class path
Processing of file paths
NSString *[email protected] "/uesrs/apple/testfile.txt"
Get the various components that make up this path, results: ("/", "User", "Apple", "testfile.txt")
-(Nsarray *) pathcomponents;
Extract the last component of the path, result: testfile.txt
-(NSString *) lastpathcomponent;
Delete the last component of the path, result:/users/apple
-(NSString *) stringbydeletinglastpathcpmponent;
Add path to the end of the first mail path, result:/users/apple/testfile.txt/app.txt
-(NSString *) Stringbyappendingpathconmponent: (NSString *) str;
Go to the last part of the path extension, result: text
-(NSString *) pathextension;
Delete the extension of the last part of the path, result:/users/apple/testfile
-(NSString *) stringbydeletingpathextension;
Append extension to the last part of the path, result:/user/apple/testfile.txt.jpg
-(NSString *) Stringbyappendingpathextension: (NSString *) str;
Third, NSData
NSData is used to package data.
NSData store binary data, shielding the difference between data, text, audio, images and other data can be used NSData to store
The use of NSData
1.NSString and NSData convert each other
Nsdata-> NSString
NSString *astring = [[NSString alloc] initWithData:adataencoding:NSUTF8StringEncoding];
Nsstring->nsdata
Convert data of type UTF8 to data
+ (NSString *) datatoutf8string: (NSData *) data{nsstring *buf = [[NSString alloc] initwithdata:data encoding: Nsutf8stringencoding];return [buf autorelease];}
Converts a string to the specified encoding
+ (NSString *) changedatatoencodinstring: (NSData *) data Encodin: (nsstringencoding) encodin{nsstring *buf = [[[ NSString Alloc] Initwithdata:data Encoding:encodin] autorelease]; return BUF;}
2. NSData and UIImage
Nsdata->uiimage
UIImage *aimage = [UIImage imagewithdata:imagedata];
Example: Taking a picture from a local file sandbox and converting it to NSData
NSString *path = [[NSBundle mainbundle] bundlepath]; NSString *name = [NSString stringwithformat:@ "Ceshi.png"]; NSString *finalpath = [path stringbyappendingpathcomponent:name]; NSData *imagedata = [NSData Datawithcontentsoffile:finalpath]; UIImage *aimage = [UIImage imagewithdata:imagedata];
3.NSData and Nsarray nsdictionary
+ (NSString *) Getlocalfilepath: (NSString *) Filename{return [nsstring stringwithformat:@ "%@/%@%@", NSHomeDirectory (), @ "Documents", FileName];}
Including writing nsdata into the documents directory
Reading data from the documents directory
In the network data communication, often encounter nsdata type of data. In the case where the data is a dictionary structure, the system does not provide an off-the-shelf method of converting to nsdictionary, which can be extended by category to Nsdictionary to support conversions from NSData to Nsdictionary. The Declaration and implementation are as follows:
+ (Nsdictionary *) Dictionarywithcontentsofdata: (NSData *) data { Cfpropertylistref list = Cfpropertylistcreatefromxmldata (Kcfallocatordefault, (cfdataref) data, kcfpropertylistimmutable, NULL); if (list = = nil) return nil; if ([(ID) List Iskindofclass:[nsdictionary class]]) { return [(nsdictionary *) List autorelease]; } else { cfrelease (list); return nil; } }
Iv. Common methods of document management
Nsfilemanager
Create a file and write data
-(BOOL) Createfileatpath: (NSString *) path contents: (NSData *) Data attributes: (Nsdictionary *) attr;
Reading data from a file
-(NSData *) Contentsatpath: (NSString *) path;
Move the files on the Scrpath path to the Dstpath path, note that the path here is the file path instead of the directory
-(BOOL) Moveitematpath: (NSString *) Srcpath Topath: (NSString *) Dstpath Error: (NSERROR *) error;
Copy files on the Scrpath path to the Dstpath path
-(BOOL) Copyitematpath: (NSString *) Scrpath Topath: (NSString *) Dstpath Error: (NSERROR *) error;
Compare the contents of two files
-(BOOL) Contentsequalatpath: (NSString *) path1 Andpath: (NSString *) path2;
Whether the file exists
-(BOOL) Fileexistsatpath: (NSString *) path;
Remove files from
-(BOOL) removeItemAtPath: (NSString *) path error: (NSERROR * *) error;
Create File Management
Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager]; NSString *path = [Nshomedirectory () stringbyappendingpathcomponent:@ "HolyBible.txt"];
Convert string to NSData type
Write file
BOOL success = [FileManager Createfileatpath:path contents:data Attributes:nil];
Create a folder
NSString *filepath = [path stringbyappendingpathcomponent:@ "HolyBible.txt"]; NSString *contect = @ "ABCDEFG"; BOOL success = [FM Createfileatpath:filepath contents:[content datausingencoding:nsutf8stringencoding] Attributes:nil ];
nsfilemanager-Read content
NSData *filedata = [FileManager Contentsatpath:filepath]; NSString *content = [[NSString alloc] Initwithdata:filedata datausingencoding:nsutf8stringencoding];
nsdata-Read content
NSString *filepath = [path stringbyappendingpathcomponent:@ "HolyBible.txt"]; NSData *data = [NSData Datawithcontentoffile:filepath];
nsstring-Read content
NSString *filepath = [path stringbyappendingpathcomponent:@ "HolyBible.txt"]; NSString *content = [[NSString stringwithcontentsoffile:filepath encoding:nsutf8stringencoding Error:nil];
Move, copy files
Move file (rename)
NSString *topath = [Nshomedirectory () stringbyappendingpathcomponent:@ "Hellogod/new Testament.txt"]; [FM Createdirectoryatpath:[topath stringbydeletinglastpathcomponent] Withintermediatedirectories:yes attributes: Nil Error:nil]; Nserror *error; BOOL issuccess = [FM moveitematpath:filepath topath:topath error:&error];
Copy file (rename)
NSString *copypath = [Nshomedirectory () stringbyappendingpathcomponent:@ "Backup/old Testament.txt"]; [FM Createdirectoryatpath:[topath stringbydeletinglastpathcomponent] Withintermediatedirectories:yes attributes: Nil Error:nil]; BOOL success = [FM Copyitematpath:topath topath:topath Error:nil];
Delete files, get file size
Determine if files exist and delete files
if ([FM Fileexistsatpath]) { if ([FM Removeitematpath:copypath]) { NSLog (@ "Remove success"); } }
Get File size
Get a dictionary of properties for a file
Get directory file information
Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager]; NSString *enupath = [Nshomedirectoty () stringbyappendingpathcomponent:@ "Test"]; Nsdictionaryenumerator *direnum = [FileManager Enumeratoratpath:enupath]; NSString *path = nil; while (path = [Direnum nextobject]}! = nil) { NSLog (@ "%@", path); }
iOS path sandbox file management