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

saving data in ios
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Saving Data in iOS

Saving Data with NSString and NSData

slide-2
SLIDE 2

Saving on iOS

Every iOS app is its own island Apps are prohibited from accessing 


  • r creating files in directories 

  • utside of its home directory

(with exceptions) Each app contains its own directory
 structure.

slide-3
SLIDE 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 files (like user generated content) Yes <App>/Documents/ Inbox Used to access outside entities such as opening an email attachment Yes <App>/Library The location for files that are not user files Yes <App>/Library/Caches Folder designated for any caching files No <App>/tmp/ Directory for temporary files. The system may purge files when app is not in use No

slide-4
SLIDE 4

User Backups

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

slide-5
SLIDE 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"];

slide-6
SLIDE 6

Writing NSData

BOOL success = [myData writeToFile:path options:NSDataWritingAtomic error:&error];

Option Description NSDataWritingAtomic Writes data in a temporary file, then exchanges the file when complete NSDataWritingWithoutOverwriting Preserves the existing file

Before writing to disk, check to see if high level APIs 
 are available to do the same thing.

slide-7
SLIDE 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 file system cache NSDataReadingMappedAlways Map the file, if possible.

slide-8
SLIDE 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

slide-9
SLIDE 9

Reading NSString

[[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];

Make sure to match the encoding of the string when
 it was written to disk The method will return a nil string if there was an
 error, fetching from disk.

slide-10
SLIDE 10

Demo

slide-11
SLIDE 11

Challenge Time