Quickstarters

How to build a backend for SwiftUI?

41min

Introduction

In this tutorial, you’ll learn how to build a complete backend for a SwiftUI iOS app using Back4App.

We'll show you how to integrate the Parse Swift SDK into your SwiftUI project and take advantage of key Back4App features—like database management, Cloud Code functions, REST and GraphQL APIs, user authentication, and real-time queries—to create a secure, scalable backend.

This approach helps you store data securely, run server side operations without setting up your own infrastructure, and easily scale your iOS app or web app as needed.

You’ll also see how Back4app’s quick setup and intuitive environment can drastically reduce the time and effort compared to manually configuring your own server and database.

By the end, you’ll understand how to build a backend for SwiftUI that can be extended to a production setting or integrated with custom logic and APIs.

Let’s get started on creating a robust, scalable backend with minimal overhead!

Prerequisites

  • A Back4app account and a new Back4app project Getting Started with Back4app. If you do not have an account, you can create one for free. Follow the guide above to get your project ready.
  • Basic SwiftUI development environment You will need Xcode installed on your machine and a basic SwiftUI or iOS app project set up.
  • Swift Package Manager or CocoaPods To add the Parse Swift SDK. Parse Swift SDK Documentation.
  • Familiarity with Swift and SwiftUI If you’re new to SwiftUI, review Apple’s SwiftUI Documentation. before starting.

Make sure these are all in place before you begin. Having your Back4app project set up and your local SwiftUI environment ready will help you follow along more easily.

Step 1 – Setting Up Back4app Project

Create a New Project

The first step in building your SwiftUI backend on Back4App is creating a new project. If you have not already created one, follow these steps:

  1. Log in to your Back4app account.
  2. Click the “New App” button in your Back4app dashboard.
  3. Give your app a name (e.g., “SwiftUI-Backend-Tutorial”).
Document image


Once the project is created, you will see it listed in your Back4app dashboard. This project will be the foundation for all backend configurations discussed in this tutorial.

Connect the Parse Swift SDK

Back4app relies on the Parse Platform to manage your data, provide real-time features, handle user authentication, and more. Connecting your SwiftUI application to Back4app involves installing the Parse Swift SDK and initializing it with the credentials from your Back4app dashboard.

Retrieve your Parse Keys: In your Back4app dashboard, navigate to your app’s “App Settings” or “Security & Keys” section to find your Application ID and Client Key (or JavaScript Key). You’ll also find the Parse Server URL (often in the format https://parseapi.back4app.com).

Document image


Install the Parse Swift SDK:

If using Swift Package Manager, open your project in Xcode and then:

  1. FileAdd Packages → enter the URL https://github.com/netreconlab/Parse-Swift.git.
  2. Choose “Up to Next Major” from the Version rules and confirm.

If using CocoaPods:

Bash


Initialize Parse in your app. For example, in your App struct:

Swift


By completing this step, you have established a secure connection between your SwiftUI front-end and the Back4app backend. All requests and data transactions are handled by the Parse Swift SDK, reducing the complexity of manual REST or GraphQL calls (though you can still use them when needed).

Step 2 – Setting Up the Database

Saving and Querying Data

With your Back4app project set up and the Parse Swift SDK integrated, you can now start saving and retrieving data. Below is a simple ParseObject example. Suppose we have a Todo struct:

Swift


Saving a new Todo:

Swift


Querying all Todos:

Swift


Alternatively, you can use Back4app’s REST API endpoints, or GraphQL:

Curl


GraphQL:

GraphQL


Having multiple options (Parse Swift SDK, REST, GraphQL) lets you choose the approach that works best for your workflow.

Schema Design and Data Types

By default, Parse allows schema creation on the fly, but you can also define your classes in the Back4app dashboard for more control.

  1. Navigate to the “Database” section in your Back4app dashboard.
  2. Create a new class (e.g., “Todo”) and add relevant columns, such as title (String) and isCompleted (Boolean).
Create New Class
Create New Class


Back4app supports various data types: String, Number, Boolean, Object, Date, File, Pointer, Array, Relation, GeoPoint, and Polygon. You can choose the appropriate type for each field.

Create Column
Create Column


Back4app also provides an AI Agent that can help you design your data model:

  1. Open the AI Agent from your App Dashboard.
  2. Describe your data model in simple language (e.g., “Please create a new ToDo class schema.”).
  3. Let the AI Agent generate the schema for you.
Document image


Relational Data

If you have relational data—say, a Category object that points to multiple Todo items—you can use Pointers or Relations in Parse. For instance:

Swift


When querying, you can also include pointer data:

Swift


Live Queries

For real-time updates, Back4app provides Live Queries. In your SwiftUI app, you can subscribe to changes in a specific class:

  1. Enable Live Queries in your Back4app dashboard under Server Settings.
  2. Subscribe in code.
Swift


Whenever a Todo is created, updated, or deleted on the server, your app gets notified in real-time. This feature is very handy for collaborative or dynamic apps that require immediate data syncing.

Step 3 – Applying Security with ACLs and CLPs

Back4app Security Mechanism

Back4app provides Access Control Lists (ACLs) and Class-Level Permissions (CLPs) for robust data security. These let you restrict who can read or write data on a per-object or per-class basis, ensuring only authorized users can modify your data.

Document image


Access Control Lists (ACLs)

An ACL is applied to individual objects. For example, to ensure only a particular user can read/write a Todo:

Swift


Class-Level Permissions (CLPs)

CLPs govern an entire class’s defaults, such as whether the class is publicly readable or writable, or if only certain roles can access it.

  1. Go to the Back4app Dashboard and select your app.
  2. Open the Database section, select a class (e.g., Todo).
  3. Open the Class-Level Permissions tab.
  4. Configure defaults like “Requires Authentication” or “No Access.”
Document image


These permissions set a broad baseline, while ACLs let you fine-tune security at the object level. Combining both ensures a strong security model. For more detail, see App Security Guidelines.

Step 4 – Writing and Deploying Cloud Functions

Cloud Code allows you to run custom server-side Swift (or JavaScript) code without managing servers. It’s ideal for adding business logic, data validations, triggers, or backend integrations that should not run from the client.

How It Works

  • Write your Cloud Code in a file like main.js (JavaScript) or leverage the Swift-based Cloud Code environment.
  • Deploy to your Back4app project. The code runs in Parse Server’s environment, so you don’t need to maintain your own server.
  • Call your Cloud Code from the client via the Swift SDK, REST, or GraphQL.

Typical Use Cases

  • Business Logic: Complex data validations, field calculations, or external integrations.
  • Data Triggers: Execute logic when an object is saved, updated, or deleted.
  • Security: Protect sensitive operations from the client by running them on the server side.

Example Function

Below is a sample JavaScript Cloud Code (since Cloud Code primarily uses JS on Back4App) that calculates text length:

main.js


Deploying via the CLI

  1. Install the Back4app CLI.
  2. Configure your account key:
Bash

  1. Deploy Cloud Code:
Bash


Calling Your Function

From Swift:

Swift


Via REST:

Curl


Step 5 – Configuring User Authentication

User Authentication in Back4app

Back4app uses the ParseUser class to handle secure authentication. Password hashing, session tokens, and data are handled automatically, letting you focus on the app’s logic.

Setting Up User Authentication

Sign Up a new user:

Swift


Log in an existing user:

Swift


Session Tokens are handled automatically by Parse. You can log out:

Swift


Social Login Integration

Integrations with Google, Apple, Facebook, etc., are possible with additional setup. Check the Social Login Docs for more details.

Email Verification and Password Reset

Enable email verification in your Back4app dashboard, and configure password reset emails so users can securely recover their accounts. For password resets:

Swift


Step 6 – Handling File Storage

Uploading and Retrieving Files

Parse includes ParseFile for handling file uploads:

Swift


Attach it to a ParseObject:

Swift


File Security

By default, files are accessible via a public URL. You can configure file security using Parse Server settings. For stricter file controls, ensure only authenticated users or specified roles can upload or retrieve files.

Step 7 – Scheduling Tasks with Cloud Jobs

Cloud Jobs allow you to schedule and run recurring tasks like cleaning data or sending emails.

JS


After deployment, schedule it via the Back4app Dashboard under Server Settings > Background Jobs.

Document image


Step 8 – Integrating Webhooks

Webhooks let your Back4app app send HTTP requests to external services (like Stripe) whenever certain events occur. This is helpful for hooking into third-party tools or other server side integrations.

  1. Go to Webhooks in your Back4app dashboard > More > WebHooks.
  2. Add an endpoint (e.g., https://your-service.com/webhook).
  3. Configure which events (e.g., new record in the Todo class) will trigger the webhook.
Adding a Webhook
Adding a Webhook


For example, you could send a Slack message whenever a user creates a new Todo. This helps you keep external services in sync with your Back4app data.

Step 9 – Exploring the Back4app Admin Panel

The Back4app Admin App is a web-based management interface for non-technical users to do CRUD operations on your data. It provides a user-friendly UI that’s separate from the Parse Dashboard, making it easier to manage data in production.

Enabling the Admin App

Go to your App Dashboard > More > Admin App and click “Enable Admin App.”

Enable Admin App
Enable Admin App


Create an Admin User, which auto-generates an admin role and associated classes. Choose a subdomain for the admin interface, then log in to manage your data via a simple point-and-click UI.

Document image


Conclusion

By following this comprehensive guide, you have learned:

  • How to build a backend for SwiftUI using Back4app and the Parse Swift SDK.
  • How to store data securely with custom class schemas and relationships.
  • How to integrate real-time queries (Live Queries) for immediate data updates.
  • How to apply security measures using ACLs and CLPs to protect and manage data access.
  • How to run business logic via Cloud Code.
  • How to handle file storage, authentication, and scheduling background tasks with Cloud Jobs.
  • How to integrate your app with external services using webhooks.

With these skills, you can build an iOS app or web app that’s easy to manage, secure, and ready to scale. Explore more advanced features—like role-based permissions, push notifications, or advanced caching. You can also integrate other APIs or tailor your Cloud Code to suit specialized use cases.

Happy coding, and enjoy building awesome apps on Back4App!

Next Steps

  • Production-Ready SwiftUI App: Extend this backend to handle more complex data models, caching, and performance enhancements.
  • Advanced Features: Dive deeper into specialized authentication, role-based access, or third-party API integrations (like Stripe).
  • Explore Back4app’s Official Docs: For deeper dives on server side security, logs analysis, and advanced database tuning.
  • Check Additional Tutorials: Real-time chat, location-based services, or multi-tenant applications—combine the methods learned here with external APIs to create sophisticated, real-world solutions.