App42 iOS SDK now supports iOS offline caching and storage that will make your iOS application responsive even if the network is not available and the user can use your application flawlessly without any trouble. iOS offline caching enables your application to read data from local cache if the network is not available. Using this feature is quite straight forward and requires a single line of code to enable it.
iOS caching data and iOS offline storage can be used in isolation or they can be used along with each other based on your app requirement. You can enable these features as explained below.
Caching allows applications to read data from its local cache depending on the policy that you have set.
App42 allows 3 different cache policies that we have discussed below.
- APP42_CACHE_FIRST: This policy enables apps to first look for the cached response for all the READ requests and returns the valid cached data if available otherwise the request will be sent to the server. Valid cached response means, if the cached response is not expired.
- APP42_NETWORK_FIRST: This policy will try to send request to the server if the network is available otherwise it will look for the cached response.
- APP42_NO_CACHE: This is a default policy and follows the normal flow i.e. it sends every request directly to the server.
Note: The expiry time for cached response is set by default to 24 hours but you can always overwrite using our API as explained below:
[code java][[App42CacheManager sharedCacheManager] setPolicy:APP42_CACHE_FIRST]; //Setting the Policy [[App42CacheManager sharedCacheManager] setExpiryInMinutes:<EXPIRY_TIME_IN_MINUTES>]; // Setting expiry time[/code]
Before talking about the details, let us go through some use cases.
- User is playing your game and the data like score needs to be saved to the cloud but there is no network.
- User wants to post his status in a social app and there is no network.
- User wants to modify his existing note but there is no network available.
There may be such use cases and as a result, the application may lose the activity of that particular user, which may be a disappointment and this is where this feature will help.
Offline Storage allows the application to save requests (POST, PUT and DELETE) locally in the application cache when it runs offline and sends them to the server when it goes online.
Enabling this feature is a single step process which is as follows.
[code java][App42API setOfflineStorage:YES]; //Enabling Offline Storage[/code]
Also Read: Tutorial on iOS Caching & Offline Storage
With the above-mentioned code of 2 lines, you have enabled two great features in your iOS app.