Was this article helpful?
Thanks for your feedback
This guide will help you get started with Contentstack iOS SDK to build apps powered by Contentstack.
To get started with iOS SDK, you will need the following:
Contentstack offers three regions US (North America, or NA), Europe (EU), and Azure NA (AZURE_NA) 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 Europe 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:
pod 'Contentstack'
pod install
You can either import the header or the module.
#import <Contentstack/Contentstack.h>
import Contentstack
@import Contentstack
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:
let stack:Stack = Contentstack.stack(withAPIKey: "API_KEY", accessToken:"DELIVERY_TOKEN", environmentName:"ENVIRONMENT")
Stack *stack = [Contentstack stackWithAPIKey:@"API_KEY" accessToken:@"DELIVERY_TOKEN" environmentName:@"ENVIRONMENT"];
For Setting the Europe Region:
If you want to set and use Europe region, refer to the code below:
var config: Config = Config(); config.region = ContentstackRegion.EU; let stack:Stack = Contentstack.stack(withAPIKey: "API_KEY", accessToken:"DELIVERY_TOKEN", environmentName:"ENVIRONMENT", config:config)
Config *config = [[Config alloc] init]; config.region = EU; Stack *stack = [Contentstack stackWithAPIKey:@"API_KEY" accessToken:@"DELIVERY_TOKEN" environmentName:@"ENVIRONMENT" config:config];
Once you have initialized the SDK, you can query entries to fetch the required content.
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.
To set the cache policy to all the query objects of an entry, refer to the code below:
let stack : Stack = Contentstack.stack(withAPIKey: "API_KEY", accessToken:"DELIVERY_TOKEN", environmentName:"ENVIRONMENT") let contentType = stack.contentType(withName: "CONTENT_TYPE_UID") let entry = contentType.entry(withUID: "ENTRY_UID") entry.cachePolicy = .NETWORK_ELSE_CACHE entry.fetch { (responseType, error) in }
Stack *stack = [Contentstack stackWithAPIKey:@"API_KEY" accessToken:@"DELIVERY_TOKEN" environmentName:@"ENVIRONMENT"]; ContentType* csForm = [stack contentTypeWithName:@"CONTENT_TYPE_UID"]; Entry *entry = [csForm entryWithUID:@"ENTRY_UID"]; [entry setCachePolicy:(NETWORK_ELSE_CACHE)]; [entry fetch:^(ResponseType type, NSError * _Nullable error) { }];
To set the cache policy to all the query objects of an asset, refer to the code below:
let stack : Stack = Contentstack.stack(withAPIKey: Contentstack.stack(withAPIKey: "API_KEY", accessToken:"DELIVERY_TOKEN", environmentName:"ENVIRONMENT") let asset = stack.asset(withUID: "ASSET_UID") asset.cachePolicy = .NETWORK_ELSE_CACHE asset.fetch { (responseType, error) in }
Stack *stack = [Contentstack stackWithAPIKey:@"API_KEY" accessToken:@"DELIVERY_TOKEN" environmentName:@"ENVIRONMENT"]; Asset* asset = [stack assetWithUID:@"ASSET_UID"]; [asset setCachePolicy:(NETWORK_ELSE_CACHE)]; [asset fetch:^(ResponseType type, NSError * _Nullable error) { }];
To set/override a cache policy on a specific query object, refer to the code below:
let stack : Stack = Contentstack.stack(withAPIKey: "API_KEY", accessToken:"DELIVERY_TOKEN", environmentName:"ENVIRONMENT") let contentType = stack.contentType(withName: "CONTENT_TYPE_UID") let query = contentType.query() query.cachePolicy = .NETWORK_ELSE_CACHE query.find { (responseType, result, error) in }
Stack *stack = [Contentstack stackWithAPIKey:@"API_KEY" accessToken:@"DELIVERY_TOKEN" environmentName:@"ENVIRONMENT"]; ContentType* contentType = [stack contentTypeWithName:@"CONTENT_TYPE_UID"]; Query* query = [contentType query]; [query setCachePolicy:(NETWORK_ELSE_CACHE)]; [query find:^(ResponseType type, QueryResult * _Nullable result, NSError * _Nullable error) { }];
Contentstack SDKs let you interact with the Content Delivery APIs and retrieve content from Contentstack. They are read-only in nature. The SDKs fetch and deliver content from the nearest server via Fastly, our powerful and robust CDN.
To retrieve a single entry from a content type, refer to the code below::
var contentType:ContentType = stack.contentType(withName: "CONTENT_TYPE_UID") var entry:Entry = contentType.entry(withUID: "ENTRY_UID") entry.fetch { (responseType, error) -> Void in }
ContentType *contentType = [stack contentTypeWithName:@"CONTENT_TYPE_UID"]; Entry *entry = [contentType entryWithUID:@"ENTRY_UID"]; [entry fetch:^(ResponseType type, NSError * _Nullable error) { }];
To retrieve multiple entries of a particular content type, refer to the code below::
var contentType:ContentType = stack.contentType(withName: "CONTENT_TYPE_UID") var query:Query = contentType.query() query.find { (responseType, result!, error!) -> Void in //error for any error description //result for response data }
ContentType *contentType = [stack contentTypeWithName:@"CONTENT_TYPE_UID"]; Query *query = [contentType query]; [query 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.
Note: For more information on how to query entries and assets, refer the Queries section of our Content Delivery API documentation.
In a single instance, the Get Multiple Entries query will retrieve only the first 100 items of the specified ContentType. You can paginate and retrieve the rest of the items in batches using the skip and limit parameters in subsequent requests.
var contentType:ContentType = stack.contentType(withName: "blog") var query:Query = contentType.query() query.limitObjects(NSNumber(int:5)) query.skipObjects(NSNumber(int:5)) query.find { (responseType, result!, error!) -> Void in //error for any error description //result for response data }
ContentType *contentType = [stack contentTypeWithName:@"blog"]; Query *query = [contentType query]; [query limitObjects:@(5)]; [query skipObjects:@(5)]; [query find:^(ResponseType type, QueryResult *result, NSError *error) { //error for any error description //result for response data }];
Was this article helpful?
Thanks for your feedback