Contentstack is a headless CMS with an API-first approach. It is a CMS that developers can use to build powerful cross-platform applications in their favorite languages. Build your application frontend, and Contentstack will take care of the rest. Read More.
Contentstack provides iOS SDK to build application on top of iOS. Given below is the detailed guide and helpful resources to get started with our iOS SDK.
Prerequisite
Latest Xcode and Mac OS X
Setup and Installation
To use this SDK on iOS platform, you will have to install the SDK according to the steps given below.
CocoaPods
Add the following to your Podfile:
use_frameworks!
pod 'ContentstackUtils', '~> 1.2.1'
Swift Package Manager
Installing libxml2 to your computer:
// macOS: For xcode 11.3 and earlier, the following settings are required.
$ brew install libxml2
$ brew link --force libxml2
// Linux(Ubuntu):
$ sudo apt-get install libxml2-dev
In the target settings add $(SDKROOT)/usr/include/libxml2 to the Search Paths > Header Search Paths field
In the target settings add $(SRCROOT)/Modules to the Swift Compiler - Search Paths > Import Paths field
Note: If you are using Contentstack Swift SDK in your project, the ContentstackUtils file is already imported.
Usage
Let’s learn how you can use Utils SDK to render embedded items.
Create Render Option
To render embedded items on the front-end, create a class implementing Option protocol, and define the UI elements you want to show in the front-end of your website, as shown in the example below:
Contentstack Utils SDK lets you interact with the Content Delivery APIs and retrieve embedded items from the RTE field of an entry.
Fetch Embedded Item(s) from a Single Entry
Render HTML RTE Embedded object
To get an embedded items of a single entry, you need to provide the stack API key, environment name, delivery token, content type and entry UID. Then, use the ContentstackUtils.render functions as shown below:
import ContentstackUtils
let stack:Stack = Contentstack.stack(apiKey: API_KEY, deliveryToken: DELIVERY_TOKEN, environment: ENVIRONMENT)
stack.contentType(uid: contentTypeUID)
.entry(uid: entryUID)
.include(.embeddedItems)
.fetch { (result: Result<EntryModel, Error>, response: ResponseType) in
switch result {
case .success(let model):
ContentstackUtils.render(content: model.richTextContent, Option(entry: model))
case .failure(let error):
//Error Message
}
}
Render Supercharged RTE contents
To get a single entry, you need to provide the stack API key, environment name, delivery token, content type and entry UID. Then, use ContentstackUtils.jsonToHtml function as shown below:
import ContentstackUtils
let stack:Stack = Contentstack.stack(apiKey: API_KEY, deliveryToken: DELIVERY_TOKEN, environment: ENVIRONMENT)
stack.contentType(uid: contentTypeUID)
.entry(uid: entryUID)
.include(.embeddedItems)
.fetch { (result: Result<EntryModel, Error>, response: ResponseType) in
switch result {
case .success(let model):
ContentstackUtils.jsonToHtml(content: model.richTextContent, Option(entry: model))
case .failure(let error):
//Error Message
}
}
Node: Supercharged RTE also supports Embedded items to get all embedded items while fetching entry use includeEmbeddedItems function.
Fetch Embedded Item(s) from Multiple Entries
Render HTML RTE Embedded object
To get embedded items from multiple entries, you need to provide the stack API key, environment name, delivery token, and content type UID. Then, use the ContentstackUtils.render functions as shown below:
import ContentstackUtils
let stack = Contentstack.stack(apiKey: apiKey,
deliveryToken: deliveryToken,
environment: environment)
stack.contentType(uid: contentTypeUID)
.entry()
.query()
.include(.embeddedItems)
.find { (result: Result<ContentstackResponse<EntryModel>, Error>, response: ResponseType) in
switch result {
case .success(let contentstackResponse):
for item in contentstackResponse.items {
ContentstackUtils.render(content: item.richTextContent, CustomRenderOption(entry: item))
}
case .failure(let error):
//Error Message
}
}
Render Supercharged RTE contents
To get a Multiple entry, you need to provide the stack API key, environment name, delivery token, and content type UID. Then, use Contentstack.Utils.jsonToHtml function as shown below:
import ContentstackUtils
let stack:Stack = Contentstack.stack(apiKey: API_KEY, deliveryToken: DELIVERY_TOKEN, environment: ENVIRONMENT)
stack.contentType(uid: contentTypeUID)
.entry()
.query()
.include(.embeddedItems)
.find { (result: Result<EntryModel, Error>, response: ResponseType) in
switch result {
case .success(let model):
for item in contentstackResponse.items {
ContentstackUtils.jsonToHtml(content: item.richTextContent, CustomRenderOption(entry: item))
}
case .failure(let error):
//Error Message
}
}
GraphQL implementation
After fetching the entries from the content type pass the JSON RTE to ContentstackUtils.GQL.jsonToHtml function as shown below:
import ContentstackUtils
import Apollo
...
let graphQLClient: ApolloClient
...
graphQLClient.fetch (query: ProductsQuery(), cachePolicy: CachePolicy.fetchIgnoringCacheData, queue: DispatchQueue.main) {[weak self] (result: Result<GraphQLResult<ProductsQuery.Data>, Error>) in
guard let slf = self else {
return
}
switch result {
case .success(let graphQLResult):
guard let data = graphQLResult.data, let products = data.allAbcd?.items else {
return
}
for product in products {
if let rte = product.superchargedRte {
let result = try? ContentstackUtils.GQL.jsonToHtml(rte: rte.resultMap)
}
}
case .failure(let error):
print("Failure! Error: \(error)")
}
}
Contentstack Swift SDK
Contentstack is a headless CMS with an API-first approach. It is a CMS that developers can use to build powerful cross-platform applications in their favorite languages. Build your application frontend, and Contentstack will take care of the rest. Read More.
Contentstack provides iOS SDK to build application on top of iOS. Given below is the detailed guide and helpful resources to get started with our iOS SDK.
Prerequisite
Latest Xcode and Mac OS X
Setup and Installation
To use this SDK on iOS platform, you will have to install the SDK according to the steps given below.
CocoaPods
Add the following to your Podfile:
Swift Package Manager
Installing libxml2 to your computer:
Add the following to your
Package.swift
:Note: When a build error occurs, please try run the following command:
Manual Installation
Kanna
Modules
$(SDKROOT)/usr/include/libxml2
to theSearch Paths > Header Search Paths
field$(SRCROOT)/Modules
to theSwift Compiler - Search Paths > Import Paths
fieldUsage
Let’s learn how you can use Utils SDK to render embedded items.
Create Render Option
To render embedded items on the front-end, create a class implementing Option protocol, and define the UI elements you want to show in the front-end of your website, as shown in the example below:
Basic Queries
Contentstack Utils SDK lets you interact with the Content Delivery APIs and retrieve embedded items from the RTE field of an entry.
Fetch Embedded Item(s) from a Single Entry
Render HTML RTE Embedded object
To get an embedded items of a single entry, you need to provide the stack API key, environment name, delivery token, content type and entry UID. Then, use the
ContentstackUtils.render
functions as shown below:Render Supercharged RTE contents
To get a single entry, you need to provide the stack API key, environment name, delivery token, content type and entry UID. Then, use
ContentstackUtils.jsonToHtml
function as shown below:Fetch Embedded Item(s) from Multiple Entries
Render HTML RTE Embedded object
To get embedded items from multiple entries, you need to provide the stack API key, environment name, delivery token, and content type UID. Then, use the
ContentstackUtils.render
functions as shown below:Render Supercharged RTE contents
To get a Multiple entry, you need to provide the stack API key, environment name, delivery token, and content type UID. Then, use
Contentstack.Utils.jsonToHtml
function as shown below:GraphQL implementation
After fetching the entries from the content type pass the JSON RTE to
ContentstackUtils.GQL.jsonToHtml
function as shown below: