iOS

Use Contentstack to power content for your iOS projects

Create iOS-based applications and use the iOS SDK to fetch and deliver content from Contentstack. The SDK uses Content Delivery APIs.

Getting Started

This guide will help you get started with our iOS SDK to build apps powered by Contentstack.

Prerequisites

To get started with iOS SDK, you will need to download the latest version of Xcode.

SDK Installation and Setup

To add the Contentstack iOS SDK to your existing project, you need to perform the steps given below:

  1. Configure the SDK:
  2. You can configure the SDK in two ways – installation using CocoaPods and manual installation.

    Method 1: Using Cocoapods

    1. Add the following line to your Podfile:
    2. pod 'Contentstack'
              
    3. Then, run the command given below to get the latest release of Contentstack.
    4. pod install
              

    Method 2: Manual method

    1. Download the iOS SDK and extract the ZIP file to your local disk.
    2. Drag and drop 'Contentstack.framework' into your project folder in Xcode. A window will appear, prompting you to choose one of the options for adding files. Click the 'Destination' checkbox to copy items into the destination group's folder. This will add the SDK to your project.
    3. In the project editor, select your app under 'TARGETS'. Under the 'General' tab, open 'Linked Frameworks and Libraries' and add the following libraries:
      • CoreGraphics.framework
      • MobileCoreServices.framework
      • Security.framework
      • SystemConfiguration.framework
    4. In your target app, click on the 'Build Settings' tab and add the '-ObjC' flag to 'Other Linker Flags'.
  3. Import header/module:
  4. You can either import the header or the module.

    1. Import the header file in Objective-C project using the command given below:
    2. #import <Contentstack/Contentstack.h>
              
    3. Import the header files as a module too:
      • Swift
      • ObjC
      import Contentstack
                      
      @import Contentstack
                      

Initialize SDK

To initialize the SDK, specify application context, the API key, Delivery token, and environment name of the stack as shown in the snippet given below:

  • Swift
  • ObjC
let stack:Stack = Contentstack.stackWithAPIKey("stack_API_key", accessToken:"access_token", environmentName:"environment_name")
        
Stack *stack = [Contentstack stackWithAPIKey:@"stack_API_key" accessToken:@"delivery_token" environmentName:@"environment_name"];
        

Once you have initialized the SDK, you can query entries to fetch the required content.

Cache Policies

The cache policies allow you to define the source from where the SDK will retrieve the content. Based on the selected policy, the SDK can get the data from cache, network, or both.

Let’s look at the various cache policies available for use:

POLICIES DESCRIPTION
NETWORK_ONLY (default) If you set NETWORK_ONLY as the cache policy, the SDK retrieves data through a network call, and saves the retrieved data in the cache. This is set as the default policy.
CACHE_ELSE_NETWORK If you set CACHE_ELSE_NETWORK as the cache policy, the SDK gets data from the cache. However, if it fails to retrieve data from the cache, it makes a network call.
NETWORK_ELSE_CACHE If you set NETWORK_ELSE_CACHE as the cache policy, the SDK gets data using a network call. However, if the call fails, it retrieves data from cache.
CACHE_THEN_NETWORK If you set CACHE_THEN_NETWORK as the cache policy, the SDK gets data from cache, and then makes a network call. (A success callback will be invoked twice.)
CACHE_ONLY If you set CACHE_ONLY as the cache policy, the SDK gets data from the cache.

You can set a cache policy on an entry, an asset, and/or a query object.

Setting a cache policy on an entry

This option allows you to set the cache policy to all the query objects of an entry.

  • Swift
  • ObjC
let stack : Stack = Contentstack.stack(withAPIKey: "blt11c1ad111ff1ddc1", accessToken: "blt33333f33333b3e3ba", environmentName: "prod")
let csForms = stack.contentType(withName: "product")
let entry = csForms.entry(withUID: "blt44fe444db44a44a4")
entry.cachePolicy = .NETWORK_ELSE_CACHE
entry.fetch { (responseType, error) in
}
        
Stack *stack = [Contentstack stackWithAPIKey:@"blt11c1ad111ff1ddc1" accessToken:@"blt33333f33333b3e3ba" environmentName:@"prod"];
ContentType* csForm = [stack contentTypeWithName:@"product"];
Entry *entry = [csForm entryWithUID:@"blt44fe444db44a44a4"];
[entry setCachePolicy:(NETWORK_ELSE_CACHE)];
[entry fetch:^(ResponseType type, NSError * _Nullable error) {
}];
        

Setting a cache policy on an asset

This option allows you to set the cache policy to all the query objects of an asset.

  • Swift
  • ObjC
let csStack : Stack = Contentstack.stack(withAPIKey: "blt11c1ad111ff1ddc1", accessToken: "blt33333f33333b3e3ba", environmentName: "env1")
let asset = csStack.asset(withUID: "blt11c1ad111ff1ddc1")
asset.cachePolicy = .NETWORK_ELSE_CACHE
asset.fetch { (responseType, error) in
}
        
Stack *csStack = [Contentstack stackWithAPIKey:@"blt12c8ad610ff4ddc2" accessToken:@"blt33333f33333b3e3ba" environmentName:@"env1"];
Asset* asset = [csStack assetWithUID:@"blt11c1ad111ff1ddc1"];
[asset setCachePolicy:(NETWORK_ELSE_CACHE)];
[asset fetch:^(ResponseType type, NSError * _Nullable error) {
}];
        

Setting a cache policy on a query object

This option allows you to set/override a cache policy on a specific query object.

  • Swift
  • ObjC
let stack : Stack = Contentstack.stack(withAPIKey: "blt11c1ad111ff1ddc1", accessToken: "blt33333f33333b3e3ba", environmentName: "prod")
let csForms = stack.contentType(withName: "product")
let query = csForms.query()
query.cachePolicy = .NETWORK_ELSE_CACHE
query.find { (responseType, result, error) in
}
        
Stack *stack = [Contentstack stackWithAPIKey:@"blt11c1ad111ff1ddc1" accessToken:@"blt33333f33333b3e3ba" environmentName:@"prod"];
ContentType* csForm = [stack contentTypeWithName:@"product"];
Query* csQuery = [csForm query];
[csQuery setCachePolicy:(NETWORK_ELSE_CACHE)];
[csQuery find:^(ResponseType type, QueryResult * _Nullable result, NSError * _Nullable error) {
}];
        

Basic Queries

Get a Single Entry

To retrieve a single entry from a content type, use the code snippets given below:

  • Swift
  • ObjC
var contentTypeObj:ContentType = stack.contentTypeWithName("blog")
//consider 'blt-sample-31' is uid of an entry of 'blog' contenttype
var entryObj:Entry = contentTypeObj.entryWithUID("blt-sample-31")
entryObj.fetch { (error!) -> Void in
  //error if exists then use 'error' object for details
}
        
ContentType *contentTypeObj = [stack contentTypeWithName:@"blog"];
//consider 'blt-sample-31' is uid of an entry of 'blog' contenttype
Entry *entryObj  = [contentTypeObj entryWithUID:@"blt-sample-31"];
[entryObj fetch:^(NSError *error) {
  //error if exists then use 'error' object for details
}];
        

Get Multiple Entries

To retrieve multiple entries of a particular content type, use the code snippets given below:

  • Swift
  • ObjC
var contentTypeObj:ContentType = stack.contentTypeWithName("blog")
var blogQuery:Query = contentTypeObj.query()
blogQuery.find { (responseType, result!, error!) -> Void in
  //error for any error description
  //result for response data
}
        
ContentType *contentTypeObj = [stack contentTypeWithName:@"blog"];
Query *blogQuery = [contentTypeObj query];
[blogQuery find:^(ResponseType type, QueryResult *result, NSError *error) {
  //error for any error description
  //result for response data
}];
        

These were the example of some of the basic queries of the SDK. For advanced queries, refer to our API reference documentation by visiting the link given below.

Using the Sync API with iOS SDK

The Sync API takes care of syncing your Contentstack data with your app and ensures that the data is always up-to-date by providing delta updates. Contentstack’s iOS SDK provides several methods that you can use to sync data. Learn more about these methods below:

Using Persistence Library with iOS SDK

The Persistence Library helps you save data on the local database of your app, and lets your app work offline as well. This library contains methods that are required to map fields of your content types and the device’s local storage (Realm).

API Reference

Go through our SDK API Reference guide to know about the methods that can be used to query your content in Contentstack.

Read iOS API Reference Guide

Download SDK

The iOS SDK interacts with and uses our Content Delivery APIs to fetch content from Contentstack. Download the SDK to deliver content to your iOS projects.

Download iOS SDK

Example Apps

To help you get started, we have created some sample applications that are powered by Contentstack iOS SDK. Click on any of the links below to read the tutorials of the app, view app demo, or download the code from GitHub.

Was this article helpful?
top-arrow