microCMS

Getting Started

This document outlines the quickest steps to use microCMS from an iOS application.
If you are using microCMS from an iOS application for the first time, please try these steps first.

Examples with microCMS Templates

There are also examples of templates for iOS in the microCMS templates.

You can quickly try out a pre-built environment with just one click, so please refer to this as well.
Notification List for iOS | microCMS Template


Below are the methods for setting it up yourself.

Things to Confirm in Advance

  • You need to understand how to register for a microCMS account, create a service, and create an API. Please refer to the operation manual for more information.
  • An iOS development environment is required. Please prepare the necessary development environment, such as Xcode, in advance.
  • You must meet the requirements for using the microcms-ios-sdk. Please ensure you understand the supported platforms and licenses before using it.


Setting Up microCMS

First, we will create a very simple API in microCMS.

  • Object format
  • One text field


Below is an example of the configuration. For detailed steps on creating the API, please refer to Creating an API.

On the API creation screen, enter a desired API name and endpoint.



Next, select the object format.


Finally, set the fields. This time, we will set only one text field.



With the above settings, the API will be created, allowing you to submit content.
Move to the editing screen, enter the desired values, and publish.



At this point, a response containing content data will be returned from the API.
Click on the API preview in the upper right corner to access the created API and confirm that a JSON response is being returned.


iOS App Development

We will develop the iOS app.
First, create an iOS project. Select "App" and then "Next".



The app name will be "HelloMicroCms".
This time, we will introduce a method using Storyboard, but it will work with almost the same code in SwiftUI.


Loading the SDK

microcms-ios-sdk is distributed via Swift Package Manager.
Select "File > Swift Package > Add Package Dependency..." from the Xcode menu.



A screen will appear to enter the URL, so input the GitHub URL of microcms-ios-sdk.

  • https://github.com/microcmsio/microcms-ios-sdk



Next, specify the version of the SDK to be used. Here, we will specify 2.2.0.
For available versions, please check the GitHub page.



Finally, a confirmation screen will appear for the target to which you want to add it, so just click Finish.



That concludes the loading of the SDK.

Fetching and Displaying Information from microCMS

We will fetch information from the microCMS API created earlier and display it on the screen.
Open Main.storyboard and add a Label to the center of the screen.



Connect the Outlet of this Label and define it with the variable name label.

class ViewController: UIViewController {
    @IBOutlet weak var label: UILabel!
}


Next, add the information retrieval process using the SDK to ViewController.swift.
This time, we will display the information at the timing of the screen display, so we will perform the process in viewDidLoad().

import UIKit

// 1. Add import statement
import MicrocmsSDK

class ViewController: UIViewController {
    @IBOutlet weak var label: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        // 2. Initialize the client
        let client = MicrocmsClient(
            serviceDomain: "<YOUR_SERVICE>",
            apiKey: "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")

        // 3. Fetch information using the SDK and display it on the screen
        client.get(endpoint: "YOUR_ENDPOINT") { [weak self] result in
            switch result {
            case .success(let object):
                if let object = object as? [String: Any],
                   let title = object["title"] as? String {
                    self?.label.text = title
                }
            case .failure(let error):
                print("[Error] \(error)")
            }
        }
    }
}


When you run this state, it will fetch the content submitted to microCMS and display it on the screen.



Also, please confirm that changing the content submitted to microCMS will change the displayed content on the screen.