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, access 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:@"access_token" environmentName:@"environment_name"];
        

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

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 reponse data
}];
        

These were 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.

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