Nsfilemanager File Manager

Source: Internet
Author: User

The sandbox mechanism for iOS, apps can only access files in their own app directory. Unlike Android, iOS does not have an SD card concept and does not have direct access to images, videos, and other content. Content generated by iOS apps, like, files, cached content, and so on, must be stored in their own sandbox. By default, each sandbox contains 3 folders: Documents, Library, and TMP. The library contains caches, preferences directories.

The full path above is: User--resource library->application Support->iphone simulator->6.1->aplications

Documents: Apple recommends that the files generated by the program creation and the file data generated by the app's browsing be saved in this directory, whichis included in itunes backup and restore
Library: The default settings or other status information of the stored program;

Library/caches: Store the cache file, save the persisted data of the application, use it to apply the upgrade or save the data after the app is closed, and not be synced by itunes, so in order to reduce the time of synchronization, consider putting some larger files into this directory without needing backup.

TMP: Provides an immediate place to create temporary files, but does not need to be persisted, after the application is closed, the data in the directory will be deleted, or the system may be cleared when the program is not running.

APP Sandbox

How does iOS get the sandbox path and how do I manipulate files? See Demo:

1- (void) Viewdidload {2 [Super Viewdidload];3     //Do any additional setup after loading the view.4Self.view.backgroundColor =[Uicolor Whitecolor];5     6 [self initdata];7 8     9 }Ten  One  A #pragmaMark--nsfilemanager File Manager - /** - * 1. Ability to create folders, create delete assignment move the 2. Through the path - 3. -  */ -  +-(void) initdata{ -     +     //file managers are a single instance of the system ANsfilemanager *filemanager =[Nsfilemanager Defaultmanager]; at      -     //Sandbox Documents folder path -NSString *documentspath =[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) firstobject]; -      -     //Sandbox caches folder path -NSString *cachespath =[Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) lastobject]; in      -     //1. Create a folder toNSString *path = [Documentspath stringbyappendingpathcomponent:@"Download/image"]; +      -     /* the Caller of method: File Manager * parameter 1: The path to the folder you want to create $ parameter 2:yes (creates a nonexistent folder under this path) No (Ignore nonexistent folder, do not create)Panax Notoginseng Parameter 3: Settings related to the folder you created - Parameter 4: Error message the      */ +  A [FileManager createdirectoryatpath:path withintermediatedirectories:yes attributes:nil Error:nil]; the      +NSLog (@"%@", path); -      $      $     //2. Determine if there is a file, if it exists, delete it, do not exist, create -      -NSString *filepath = [Cachespath stringbyappendingpathcomponent:@"Xiaopingguo.mp3"]; the      -     //determine if a corresponding file exists under this pathWuyi     if([FileManager Fileexistsatpath:filepath]) { the          -         //exists Delete Wu [FileManager Removeitematpath:filepath error:nil]; -NSLog (@"document exists, delete"); About     } $      -     Else -     { -         //does not exist create A [FileManager Createfileatpath:filepath contents:nil Attributes:nil]; +          theNSLog (@"file does not exist, create"); -          $     } the      theNSLog (@"%@", FilePath); the      the      -  in     //3. Assigning Values the      the[FileManager copyitematpath:filepath Topath:[documentspath stringbyappendingpathcomponent:@"Xiaopingguo.mp3"] Error:nil]; AboutNSLog (@"%@", Documentspath); the      the      the     //4. Mobile +     /* - parameter 1: Source path of the file the Parameter 2: Destination path, need to splice the file nameBayi Parameter 3: Error message the      */ the      -[FileManager moveitematpath:filepath Topath:[path stringbyappendingpathcomponent:@"Dabaojian.mp4"] Error:nil]; -      the      the     //5. Get the file's sub-file theNsarray *filearray =[FileManager Contentsofdirectoryatpath:documentspath error:nil]; the      -NSLog (@"%@", Filearray); the  the      the}

Nsfilemanager File Manager

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.