Get Started with iOS SDK

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

Prerequisites

To get started with iOS SDK, you will need the following:

  • Latest version of Xcode
  • objC and Swift 4.2
  • iOS 10 or later.

SDK Installation and Setup

Contentstack offers two regions (US and European) as data centers to store customers' account details and data. Both regions are independent of each other and therefore have a dedicated set of instructions to use SDKs offered by Contentstack.

To use SDKs for the European region, you will have to make certain changes in the configuration of the SDK, as detailed below, and the rest of the instructions remain the same.

To add the Contentstack iOS SDK to your existing project, perform the following steps:

  1. Configure the SDK:
    You can configure the SDK installation using CocoaPods.

    Using Cocoapods

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

    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 stack’s API key, Delivery token, and name of the environment where you will publish the content, as shown in the snippet below:

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

For Setting the European Region:

If you want to set and use European region, refer to the code below:

  • Swift
  • ObjC
var config: Config = Config();
config.region = ContentstackRegion.eu;
let stack:Stack = Contentstack.stackWithAPIKey("API_key",
accessToken:"delivery_token", 
environmentName:"environment_id", 
config:config)
Config *config = [[Config alloc] init];
config.region = EU;
Stack *stack = [Contentstack stackWithAPIKey:@"API_key" 
accessToken:@"delivery_token" 
environmentName:@"environment_id" 
config:config];

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:

POLICIESDESCRIPTION
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_NETWORKIf 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_CACHEIf 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_NETWORKIf 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_ONLYIf 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

To set the cache policy to all the query objects of an entry, refer to the code below:

  • 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

To set the cache policy to all the query objects of an asset, refer to the code below:

  • 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

To set/override a cache policy on a specific query object, refer to the code below:

  • 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, refer to the code 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, refer to the code 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 examples of some of the basic queries of the iOS SDK. For advanced queries, refer to the Contentstack iOS API reference.

On This Page

top-arrow