Headless CMS for Swift
Hygraph is the ideal Headless CMS for Swift websites and applications. Read further to learn how our API-first CMS allows you to add components to your Swift apps in minutes and enable your website's content to be managed from a powerful CMS.
Step #1 - Set up the GraphQL client
First, you set up the GraphQL client, often using a library like Apollo iOS, which requires you to initialize the client with the API's URL.
import Apolloclass Network {static let shared = Network()private(set) lazy var apollo = ApolloClient(url: URL(string: "https://api-<region>.hygraph.com/v2/<some hash>/master")!)}
Step #2 - Define the query and fetch the data
Next, you'll define the query and fetch the data from the API. Usually the query should be defined in a separate .graphql file, however, you can also define it directly in the Swift code.
The DataService
singleton class defines a fetchData
function that executes a query and handles the asynchronous result. On success, it extracts the data and passes it to the completion handler. On failure, it consolidates GraphQL errors into a single message or relays network errors, invoking the handler with the appropriate error.
import Foundationimport Apolloclass DataService {static let shared = DataService()func fetchData(completion: @escaping (Result<YourDataType, Error>) -> Void) {Network.shared.apollo.fetch(query: YourDataQuery()) { result inswitch result {case .success(let graphQLResult):if let data = graphQLResult.data {// Handle your datacompletion(.success(data))} else if let errors = graphQLResult.errors {// GraphQL errorslet message = errors.map { $0.localizedDescription }.joined(separator: "\n")completion(.failure(NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey: message])))}case .failure(let error):// Network or response format errorscompletion(.failure(error))}}}}
Construct the UI using SwiftUI or UIKit
Finally, you can use construct the UI with the data you received from the GraphQL API. It presents a list of items fetched from the GraphQL API. The view uses the .onAppear
modifier to trigger data fetching when it appears on screen.
In case of an error, an alert is presented to the user. The view automatically updates to reflect the current state of the data, demonstrating SwiftUI's declarative UI approach where views are a function of their state.
struct ContentView: View {@State private var data: YourDataType?@State private var errorMessage: String?var body: some View {NavigationView {List(data?.items ?? [], id: \.id) { item inText(item.name)}.navigationTitle("Items").onAppear {DataService.shared.fetchData { result inswitch result {case .success(let data):self.data = datacase .failure(let error):self.errorMessage = error.localizedDescription}}}.alert(isPresented: .constant(errorMessage != nil), content: {Alert(title: Text("Error"), message: Text(errorMessage ?? "Unknown error"), dismissButton: .default(Text("OK")))})}}}
Start building with Swift
We made it really easy to set up your project in Hygraph and use our GraphQL API within your Swift project.
Quickstart
Check out our docs to see how you can quickly set up your Hygraph project and enable the content API for your Swift app.
Learn GraphQL
Hygraph is GraphQL-native Headless CMS offers precise data retrieval, minimizing over-fetching and optimizing efficiency.
Examples
Look at some of the example projects to see Hygraph in action.
Why Hygraph
Choosing Hygraph for your Swift project
Using a GraphQL-native headless CMS with a Swift application benefits developers through GraphQL's efficient data queries, enabling precise and fast data retrieval and simplifying development. Content editors get an intuitive interface for easy content management, independent of the app’s technical details.
This combination promotes quick content updates and a smooth user experience, enhancing the overall responsiveness of iOS applications.
Developer Experience
We try to be the most un-opinionated CMS on the market with a wide collection of open source example projects to get you started.
Headless CMS
As a headless CMS (i.e. API based content management), you can be as modular and flexible as you need. We even support multiplatform content management.
Management API
Hygraph boasts a flexible and powerful management API to manage your content and schema, as well as a blazing fast content API.