Offline data storage in iOS

Shubham Jain
4 min readApr 17, 2018

+ShUBHAm JAin

In this blog we will discuss offline / local iOS data storage guidelines for iOS apps intended to keep certain information locally. Local storage is meant for retaining web app data locally using certain frameworks, tools and methods distinctive to different platforms. For iOS storage there are different methods to choose from. The choice, however, depends upon what and how much data you want to store. Most of the times more than one method is required to implement local storage in iOS apps, as there are different persistence needs of the application viz. data gathered from web browsing, user preferences, and application settings. The most widely used methods for local storage implementation in iOS are:

  • SQLite
  • Property List
  • Core Data
  • NSUser Defaults
  • Key Chain

Different methods serve different data persistence purposes, for example, SQLite is used for low-level relational database work while Core Data is a modeling framework for object oriented Cocoa Touch Applications.

Guidelines for Local iOS Applications Data Storage

SQLite For iOS Local Data Storage
SQLite is a powerful lightweight C library that is embedded in an iOS application. This is used in various applications across various platforms including Android and iOS. It uses SQL-centric API to operate the data tables directly. Using SQLite C library for offline or local data storage implementation in iOS applications, user needs to be very meticulous when passing in strings and arguments required for the functions. SQLite is very particular about the argument type given to functions.

Property List
Next most common used method of offline storing data in iOS application is in Property List files. Documents in property list contain either an NSDictionary or an NSArray, inside which there is archived data.
There are number of classes that can be archived into the PList, eg. NSArray, NSDate, NSString, NSDictionary and NSdictionary. Objects other than these cannot be archived as a property list and will not be able to write the file.
user has to be very particular about listing items into the classes, for instance to store a Boolean or Integer object only NSNumber class is used. Boolean or Integer object must not be given to any objects in NSDictionary or NSArray.

Core Data
Core Data is the method recommended by Apple for local storage of app’s data. By default, core data uses SQLite as its main database in the iOS app.
Internally Core Data make use of SQLite queries to save and store its data locally, which is why all the files are stored as .db files.
This also eliminates the need to install a separate database. In iOS this framework allows for two different database storage types but by default it is SQLite.
Core Data allows you deal with common functionalities of an app like, restore, store, undo and redo.

Core data is a data modelling framework built on the model-view-controller(MVC) pattern. It is expressed in terms of entities and relationships. We can create entities and add relationships in the graphical form. This helps to eliminate most of the basic code and can be easily added into your application.

Core data provides an API to deal with common functionalities such as Save, Delete, Undo etc. which allows us to build applications easily. Using this we need not worry about SQL query and its syntax

NSUserDefaults :
To save properties in any application and user preferences, NSUserDefaults is one of the most common methods for local data storage. This is used to save logged in state of the user within an application, so that the app can fetch this data even when user access the application at some other time. In some of the iOS apps this method is used to save user’s confidential information like access token.

NSUserDefault is used to store small amounts of data. It allows the application to set and get its preferences and also allows the user to store it in a file. The stored values can be accessed globally within the whole application.

NSUserDefault caches the information. This helps the user avoid opening default databases, each time for default values.

How to store data in user preference

NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];[standardUserDefaults setObject:@”zenshubham09@blogspot.in” forKey:@”iOSBlogger”]; [standardUserDefaults synchronize];

How to fetch data from user preference

NSString *blogName = [standardUserDefaults objectForKey:@”iOSBlogger”];

Key Chain :

Most of the time developers avoid implementing key chain method to save data as this method follows a complicated procedure.
If the device is jail broken, none of your data is secure. This is the most secure and reliable method to store data on a non-jailbroken device.
Simple wrapper classes are used to store data using key chain method. The code for saving data is similar to the code for saving data in NUserDefaults and the syntax used is very similar to that of NUserDefaults.

Using these methods developers can also reduce the vulnerabilities of iOS applications, by storing local data in a secure manner.
However, data storage using the keychain is not possible for a jailbroken device, where the attacker can access everything from PList files, key chain and other method implementations.

Using the methods listed above developers can make it tricky for attackers to reach the locally stored information. Also, iOS local storage increases the offline capabilities of a mobile application, thus making it less internet dependent.

Note : Core data is a data modeling framework for object-oriented Cocoa touch applications, while SQLite is a low-level relational database and NSUserDefaults is a quick and easy way to store small amounts of data.

Thanks

Previous Post : iOS interview Question
https://medium.com/@zenshubham09/ios-interview-question-2af44bc7e748

iOSBlogger :
https://zenshubham09.blogspot.in/2018/04/offline-data-storage-in-ios.html

--

--