What is Supabase and How to Integrate It into Your iOS App?

9 min read
Emine Çetinkaya
Cover image for What is Supabase and How to Integrate It into Your iOS App?

Supercharge Your iOS App with Supabase: A Powerful and Flexible Backend Solution

In today's fast-paced world of mobile development, building apps has become quicker than ever, but have you ever wondered what happens behind the scenes, in the backend? How do apps manage to store data securely, authenticate users, and handle file uploads effortlessly? And more importantly, how can developers like you create scalable and efficient backends without spending weeks or months? Let’s explore these questions together and dive into Supabase, a powerful, open-source alternative to Firebase that offers a seamless solution for modern app development.

1. What is Supabase?

Supabase stands out as an open source Firebase alternative. It is a cloud-based Backend-as-a-Service (BaaS) platform that aims to accelerate modern application development processes. Supabase, which runs on a PostgreSQL database, provides all the basic backend services required for a web or mobile application. With these features, it is especially preferred in rapid prototyping and low-cost application development processes.

Why Supabase? 

* Open Source 

While popular services such as Firebase are closed source, Supabase is completely open source. This offers significant advantages to developers: 

  • Access to Supabase’ s source codes allows you to customize the platform to fit your project perfectly. While this is not possible on closed source platforms, with Supabase, you can customize certain components in the project if necessary. 

  • Another advantage of being open source is that a worldwide developer community constantly contributes. This means that Supabase is a constantly updated and improved platform. 

  • You can integrate Supabase onto your own servers, giving you full control over where your data is stored. You can add your own security layers to further enhance security. 

* The Power of a PostgreSQL- Powered Backend

Supabase, with its PostgreSQL-powered infrastructure, provides a solid and scalable foundation for modern web and mobile applications. But have you ever wondered what powers these applications behind the scenes? Let's dive into how Supabase’s robust infrastructure accelerates your development process and supports efficient CRUD operations.

Supabase stands out because it’s based on PostgreSQL, which brings significant flexibility and power to the platform:

  • Relational Database Power: Unlike Firebase, which uses a NoSQL database, PostgreSQL is a relational database. This is especially beneficial when you need to establish complex relationships between data or perform advanced data analysis.

  • The Power of SQL: PostgreSQL supports full SQL queries, allowing developers to perform a wide range of operations, from simple CRUD operations to complex JOINs and subqueries. In contrast, NoSQL databases like Firebase often do not provide this level of sophistication and flexibility.

  • JSON Support: PostgreSQL offers support for the JSON data type, enabling the storage of unstructured data while still benefiting from a relational database’s features. This hybrid model is perfect when you need flexibility in data storage while maintaining the power of SQL.

* Integrated Services 

Supabase doesn’t just offer a database; it offers a range of integrated services to meet your application’s backend needs: 

  • Authentication: Supabase offers a variety of authentication methods, including OAuth providers (like Google, GitHub), email and password login, and even magic link authentication. This makes user management easy and secure.

  • Storage: Supabase offers the file storage solution your application needs. You can securely store user data like photos, videos, and documents, and provide access and management to these files from a simple interface or via API.

  • Automatic API Creation: It automatically creates RESTful APIs for each table you create in the database. This allows you to exchange data between the frontend and backend quickly and effortlessly. In particular, not having to manually create the API structure significantly shortens application development time.

  • Real-Time Data: Supabase’ s real-time feature ensures that changes in the database are instantly reflected in your application. This feature is especially suitable for projects that require chat applications, live data panels, or instant updates.

  • Authorization and Security: Supabase offers advanced authorization methods such as Row Level Security (RLS) to easily manage data access and security. In this way, you can develop more secure applications by limiting data access on a per-user basis.
     

2. First Steps with Supabase: Setup and Preparation

In this step, we will show you how to sign up for the Supabase platform and integrate it with your iOS project. 

1. Creating a Supabase Account:

  • First, go to the Supabase website and create a free account.

  • Create a project and save the API key and URL information. These are required to connect your iOS app to Supabase.

2. Adding the Supabase SDK to your iOS Project: 

  • You can use CocoaPods or Swift Package Manager to add the Supabase SDK to your iOS Project.

- Installation with CocoaPods:

pod 'Supabase', '~> 0.1' 

- Installation with Swift Package Manager:

In your Xcode, go to File > Swift Packages > Add Package Dependency and add Supabase's GitHub repository: 

https://github.com/supabase/supabase-swift 

Once this process is complete, your project can now communicate with Supabase. 

3. Authentication with Supabase 

Many applications require users to register or log in. Supabase provides easy email authentication and identity management. 

- User Registration: First, let's register a user:

import Supabase 

let client = SupabaseClient(supabaseURL: "YOUR_SUPABASE_URL", supabaseKey: "YOUR_SUPABASE_KEY")
client.auth.signUp(email: "[email protected]", password: "password") { result in 
   switch result {
   case.success(let session): 
       print("Registration successful: \(session)")
   case.failure(let error):
       print("Registration error: \(error)")
   }
}

- User Login: Let’ s log in a registered user:

client.auth.signIn(email: "[email protected]", password: "password") { result in
switch result {
case.success(let session):
     print("Login successful: \(session)")
case.failure(let error):
     print("Login error: \(error)")
     }
}

You can perform simple authentication operations with these methods. You can also add features such as password reset and email verification. 

4. Data Management with Supabase: CRUD Operations 

Since Supabase offers a database running on PostgreSQL, it is very easy to 

perform CRUD operations (Create, Read, Update, Delete). Now we will show you how to perform these operations in your iOS application. 

  1. Adding Data: Let’ s add a task to a todos table:

client.from("todos").insert(values: ["task": "Learn Supabase", "is_complete": false]) {
result in 
    switch result {
    case.success(let response):
       print("Data added: \(response)")
    case.failure(let error): 
       print("Error: \(error)")
       }
}
  1. Fetching Data: To query the tasks in the table:

client.from("todos").select().execute { result in
   switch result {
   case.success(let response):
       print("Data: \(response)")
   case.failure(let error):
       print("Error: \(error)")
   }
}
  1. Data Update: To update a task:

client.from("todos").update(values: ["is_complete": true]).eq(column: "task",
value: "Learn Supabase").execute { result in
     switch result {
     case.success(let response):
         print("Mission updated: \(response)")
     case.failure(let error):
         print("Error: \(error)")
         }
}
  1. Deleting Data: Deleting a record ford:

client.from("todos").delete().eq(column: "task", value: "Learn Supabase").execute { result in
    switch result {
    case.success(let response):
        print("Task deleted: \(response)")
    case.failure(let error): 
        print("Error:\(error)")
        }
}

5. Uploading Files with Supabase’ s Storage Feature 

File uploading operations are also very important in your applications. Supabase’s We will show you step by step how to upload and manage your users' photos or files with the storage service it offers. 

client.storage.from("avatars").upload(filePath: "LocalPath/toImage.png",
to: "public/image.png") { result in
    switch result {
    case.success(let response):
       print("File uploaded:\(response)")
     case.failure(let error):
       print("Loading error:\(error)")
       }
}

This way you can upload files and access them later. 

6. Real Time Data Management

Supabase, supports real-time data management. When there is any data change in a table, you can instantly reflect this change in your application. For example, let's follow the tasks added to the todos table in real time. 

client.from("todos").on(.insert) { event in
    print("New mission added:\(event.newRecord)")
}

With this feature, you can implement real-time updates quickly and easily. 

7. Conclusion

Supabase offers fast and easy backend solutions for developers. With features such as authentication, database management, file storage and real-time data tracking, you can quickly develop powerful applications. Supabase's powerful PostgreSQL infrastructure and open source structure provide developers with flexibility and provides control.

8. Resources

You can use the following resources to learn more about Supabase and develop your applications. You can find guides and community resources below to help you better understand the features and integrations offered by Supabase: 

  • Supabase Official Documentation: All features and usage of Supabase Official documentation where you can find detailed information about. 

https://supabase.com/docs 

  • Supabase GitHub Repository: You can access Supabase's open source codes via GitHub. You can examine the project and contribute here. 

https://github.com/supabase 

  • Supabase Community Forum: The Supabase community Forum where you can interact, ask questions, and get feedback on projects. 

https://github.com/orgs/supabase/discussions 

Latest Blog Posts

Cover image for WWDC24 - Apple Intelligence
WWDC24 - Apple Intelligence
Apple Intelligence is a personal intelligence system that brings powerful generative models right to the core of your iPhone, iPad, and Mac.
September 12, 2024
Cover image for Next.js Routing: Intercepting Routes
Next.js Routing: Intercepting Routes
Next.js intercepting routes allows developers to modify or block navigation based on conditions, making it ideal for managing complex logic like authentication or loading states before accessing specific pages.
September 13, 2024