Getting Started with Contentful and Swift
This guide will show you how to get started using our Swift client library to consume content.
Contentful’s Content Delivery API (CDA) is a read-only API for retrieving content from Contentful. All content, both JSON and binary, is fetched from the server closest to a user’s location by using our global CDN.
We publish client libraries for various languages to make developing applications easier.
Requirements
This tutorial assumes that you understand the Contentful data model.
Authentication
For every request, clients need to provide an API key, which is created per space and used to delimit applications and content classes.
You can create an access token using the Contentful web app or the Content Management API.
Install the client library
There are different ways to integrate the client library into your own apps, described in detail in the README for the client library. This guide will use CocoaPods, the dependency manager for Cocoa projects, which helps you keep the client library up-to-date:
Create a Podfile for your project which adds Contentful as a dependency for you project. You can specify a particular version(s) of the client library using the various operators that Cocoapods offers
Initialize the client
You need an API key and a space ID to initialize a client. You can use the API key and space ID pre-filled below from our example space or replace them with your own values.
Accessing data
The Client class manages all requests to the API, and now that you have initialized a Client instance, you can fetch entries. Note that you must retain your Client instance as a property (instance variable) in your application so that asynchronous callbacks are not released by the system before execution.
Fetch one entry:
Fetch all entries with content_type = “cat”
A more refined approach
While the above approach is great for quickly fetching data from the API, it can also be useful to retrieve already serialized instances of your own model classes. The EntryDecodable protocol enables said mapping. EntryDecodable actually extends the Decodable protocol which is standard library starting in Swift 4, so all the methods for deserializing your fields should look familiar, and the Contentful Swift client library extends Swift’s native protocols to make things easier.
Let’s get started by implementing a model class, Cat, which will conform to both the EntryDecodable and FieldKeysQueryable protocols:
Note: If you want to simplify the implementation of an EntryDecodable, declare conformance to resource, add let sys: Sys to the class definition, and assign that property with sys = try decoder.sys() during initialization. Then, id, localeCode, updatedAt, and createdAt are all provided via the sys property and don’t need to be declared as class members. Note that this style of implementation may make integration with local database frameworks more cumbersome.
Now take a look at what a query on our Cat class would look like. In particular, we’ll create a query where the color field of our cat is set to “gray”.