saving data in ios
play

Saving Data in iOS Saving Data with NSString and NSData Saving on - PowerPoint PPT Presentation

Saving Data in iOS Saving Data with NSString and NSData Saving on iOS Every iOS app is its own island Each app contains its own directory structure. Apps are prohibited from accessing or creating fi les in directories outside of


  1. Saving Data in iOS Saving Data with NSString and NSData

  2. Saving on iOS Every iOS app is its own island Each app contains its own directory 
 structure. Apps are prohibited from accessing 
 or creating fi les in directories 
 outside of its home directory (with exceptions)

  3. Standard iOS Directories Directory Description Backed Up? <App>/AppName.app The app itself. Read only and signed to prevent tampering No <App>/Documents/ User documents and data fi les (like user generated content) Yes <App>/Documents/ Used to access outside entities such as opening an email Yes Inbox attachment <App>/Library The location for fi les that are not user fi les Yes <App>/Library/Caches Folder designated for any caching fi les No Directory for temporary fi les. The system may purge fi les <App>/tmp/ No when app is not in use

  4. User Backups Large fi les can slow the backup process in iTunes and 
 iCloud. If you need prevent fi les from being backed up, use 
 use either NSURLIsExcludedFromBackupKey or 
 kCFURLIsExcludedFromBackupKey .

  5. Accessing File Paths Use a Foundation convenience function to generate path NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSDocumentDirectory, NSLibraryDirectory, 
 NSCachedDirectory, NSTemporary Directory NSString * dirPath = [paths firstObject]; NSString * path = [dirPath stringByAppendingPathComponent:@"my_file.txt"];

  6. Writing NSData BOOL success = [myData writeToFile:path options:NSDataWritingAtomic error:&error]; Option Description Writes data in a temporary fi le, then exchanges the fi le when NSDataWritingAtomic complete NSDataWritingWithoutOverwriting Preserves the existing fi le Before writing to disk, check to see if high level APIs 
 are available to do the same thing.

  7. Reading NSData [NSData alloc] initWithContentsOfFile:path options:NSDataReadingMappedIfSafe error:&error]; Option Description NSDataReadingMappedIfSafe Should try and map the data into virtual memory, if safe. NSDataReadingUncached File should not be stored in the fi le system cache NSDataReadingMappedAlways Map the fi le, if possible.

  8. Writing NSString BOOL success = [myString writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error]; Option Description NSUTF8StringEncoding Standard unicode encoding NSASCIIStringEncoding ASCII encode with 8-bit characters If in doubt, write to NSUTF8StringEncoding. See the 
 following to learn more: http://www.objc.io/issue-9/unicode.html

  9. Reading NSString [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error]; The method will return a nil string if there was an 
 error, fetching from disk. Make sure to match the encoding of the string when 
 it was written to disk

  10. Demo

  11. Challenge Time

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend