Files
Charis 985ed13456 docs(quickstarts): add rls policy step (#28196)
Quick starts are missing the step to create an RLS policy, which can be
confusing if you choose to create the table from the Table Editor, and
subsequently can't select from it.
2024-07-31 14:58:37 +00:00

138 lines
3.3 KiB
Plaintext

---
title: 'Use Supabase with iOS and SwiftUI'
subtitle: 'Learn how to create a Supabase project, add some sample data to your database, and query the data from an iOS app.'
breadcrumb: 'Framework Quickstarts'
hideToc: true
---
<StepHikeCompact>
<StepHikeCompact.Step step={1}>
<QuickstartDbSetup />
</StepHikeCompact.Step>
<StepHikeCompact.Step step={2}>
<StepHikeCompact.Details title="Create an iOS SwiftUI app with Xcode">
Open Xcode > New Project > iOS > App. You can skip this step if you already have a working app.
</StepHikeCompact.Details>
</StepHikeCompact.Step>
<StepHikeCompact.Step step={3}>
<StepHikeCompact.Details title="Install the Supabase client library">
Install Supabase package dependency using Xcode by following Apple's [tutorial](https://developer.apple.com/documentation/xcode/adding-package-dependencies-to-your-app).
Make sure to add `Supabase` product package as dependency to the application.
</StepHikeCompact.Details>
</StepHikeCompact.Step>
<StepHikeCompact.Step step={4}>
<StepHikeCompact.Details title="Initialize the Supabase client">
Create a new `Supabase.swift` file add a new supabase instance using your project URL and public API (anon) key:
<ProjectConfigVariables variable="url" />
<ProjectConfigVariables variable="anonKey" />
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```swift Supabase.swift
import Supabase
let supabase = SupabaseClient(
supabaseURL: URL(string: "YOUR_SUPABASE_URL")!,
supabaseKey: "YOUR_SUPABASE_ANON_KEY"
)
```
</StepHikeCompact.Code>
</StepHikeCompact.Step>
<StepHikeCompact.Step step={5}>
<StepHikeCompact.Details title="Create a data model for countries">
Create a decodable struct to deserialize the data from the database.
Add the following code to a new file named `Country.swift`.
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```swift Supabase.swift
struct Country: Decodable, Identifiable {
let id: Int
let name: String
}
```
</StepHikeCompact.Code>
</StepHikeCompact.Step>
<StepHikeCompact.Step step={6}>
<StepHikeCompact.Details title="Query data from the app">
Use a `task` to fetch the data from the database and display it using a `List`.
Replace the default `ContentView` with the following code.
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```swift ContentView.swift
struct ContentView: View {
@State var countries: [Country] = []
var body: some View {
List(countries) { country in
Text(country.name)
}
.overlay {
if countries.isEmpty {
ProgressView()
}
}
.task {
do {
countries = try await supabase.from("countries").select().execute().value
} catch {
dump(error)
}
}
}
}
```
</StepHikeCompact.Code>
</StepHikeCompact.Step>
<StepHikeCompact.Step step={7}>
<StepHikeCompact.Details title="Start the app">
Run the app on a simulator or a physical device by hitting `Cmd + R` on Xcode.
</StepHikeCompact.Details>
</StepHikeCompact.Step>
</StepHikeCompact>