You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Install the library using the Swift Package Manager.
27
13
28
-
Add the following lines to your `Package.swift` file:
29
14
```swift
30
15
letpackage=Package(
31
16
...
32
17
dependencies: [
33
18
...
34
-
.package(name: "Supabase", url: "https://github.com/supabase/supabase-swift.git", branch: "master"), // Add the package
19
+
.package(name: "Supabase", url: "https://github.com/supabase/supabase-swift.git", from: "1.0.0"), // Add the package
35
20
],
36
21
targets: [
37
22
.target(
@@ -44,306 +29,35 @@ let package = Package(
44
29
45
30
If you're using Xcode, [use this guide](https://developer.apple.com/documentation/swift_packages/adding_package_dependencies_to_your_app) to add `supabase-swift` to your project. Use `https://github.com/supabase/supabase-swift.git` for the url when Xcode asks.
46
31
47
-
## Usage
32
+
If you don't want the full Supabase environment, you can also add individual packages, such as `Functions`, `GoTrue`, `Realtime`, `Storage`, or `PostgREST`.
48
33
49
-
To make requests to the `Supabase` database, you will need to initialize a `SupabaseClient` object:
34
+
Then you're able to import the package and establish the connection with the database.
/// Create a single supabase client for interacting with your database
38
+
let client =SupabaseClient(supabaseURL: URL(string: "https://xyzcompany.supabase.co")!, supabaseKey: "public-anon-key")
53
39
```
54
40
55
-
## Login Implementation
56
-
57
-
Inside the `SupabaseClient` instance created before, you can find an `auth` property of type `GoTrueClient`. You can use it to perform sign in and sign up requests.
58
-
59
-
- Here's how to sign up with an email and password and get the signed in user `Session` info:
If you wish to add metadata for the user, you can pass it as part of the `data` parameter, just be sure to `import GoTrue` first to use the User metadata values.
74
-
75
-
```swift
76
-
Task {
77
-
do {
78
-
tryawait client.auth.signUp(
79
-
email: email,
80
-
password: password,
81
-
data: [
82
-
"name": .string("John Doe"),
83
-
"age": .number(25),
84
-
"some_boolean_parameter": .bool(true)
85
-
]
86
-
)
87
-
88
-
let session =tryawait client.auth.session
89
-
print("### Session Info: \(session)")
90
-
} catch {
91
-
print("### Sign Up Error: \(error)")
92
-
}
93
-
}
94
-
```
95
-
96
-
- For existing users, here's how to log in with an email and password and get the logged in user `Session` info:
- Handle the callback `URL` on `SceneDelegate` (for older projects, you can use `AppDelegate` if `SceneDelegate` is not present).
161
-
- Post a `NotificationCenter` call to let the `ViewController` know the callback has been fired and pass the `URL` received. This `URL` will be used to get the user session.
- If using a WebViews, other social logins will be similar to above. Just follow the [Supabase's Documentation](https://supabase.com/docs/guides/auth/) for their setup.
218
-
219
-
## Basic CRUD Implementation
220
-
221
-
First, import and initialize `SupabaseClient`, as explained in "Usage" section.
222
-
223
-
### Insert Data
224
-
225
-
- You can either use `Codable` or `Encodable` and `Decodable` protocols for the model's struct. However without either, you will get an error saying `Cannot convert value of type 'Void' to specified type 'InsertModel'` when trying to cast the response to your model.
226
-
- Create a model which follows your table's data structure:
227
-
228
-
229
-
```swift
230
-
structInsertModel: Encodable, Decodable {
231
-
let id: Int?// you can choose to omit this depending on how you've setup your table
232
-
let title: String?
233
-
let desc: String?
234
-
}
235
-
236
-
let insertData =InsertModel(title: "Test", desc: "Test Desc")
237
-
let query = client.database
238
-
.from("{ Your Table Name }")
239
-
.insert(values: insertData,
240
-
returning: .representation) // you will need to add this to return the added data
241
-
.select(columns: "id") // specifiy which column names to be returned. Leave it empty for all columns
242
-
.single() // specify you want to return a single value.
243
-
244
-
Task {
245
-
do {
246
-
let response: [InsertModel] =tryawait query.execute().value
247
-
print("### Returned: \(response)")
248
-
} catch {
249
-
print("### Insert Error: \(error)")
250
-
}
251
-
}
252
-
```
253
-
254
-
### Select Data
255
-
256
-
- Using the same model as before:
257
-
258
-
```swift
259
-
let insertData =InsertModel(title: "Test", desc: "Test Desc")
260
-
let query = client.database
261
-
.from("{ Your Table Name }")
262
-
.select() // keep it empty for all, else specify returned data
returning: .representation) // you will need to add this to return the updated data
287
-
.select(columns: "id") // specifiy which column names to be returned. Leave it empty for all columns
288
-
.single() // specify you want to return a single value.
289
-
290
-
Task {
291
-
do {
292
-
let response: [InsertModel] =tryawait query.execute().value
293
-
print("### Returned: \(response)")
294
-
} catch {
295
-
print("### Update Error: \(error)")
296
-
}
297
-
}
298
-
```
299
-
300
-
### Delete Data
301
-
302
-
```swift
303
-
let query = client.database
304
-
.from("{ Your Table Name }")
305
-
.delete(returning: .representation) // you will need to add this to return the deleted data
306
-
.match(
307
-
query: ["id":1] // assuming the record above was inserted with id 1
308
-
// You can add additional conditions here
309
-
)
310
-
.select() // specifiy which column names to be returned. Leave it empty for all columns
311
-
.single()
312
-
313
-
Task {
314
-
do {
315
-
let response: [InsertModel] =tryawait query.execute().value
316
-
print("### Returned: \(response)")
317
-
} catch {
318
-
print("### Delete Error: \(error)")
319
-
}
320
-
}
321
-
```
322
-
323
-
## Postgres Functions
324
-
325
-
- Unlike the JavaScript library, you can't use the `rpc` method on the `SupabaseClient`, instead you need to use the `rpc` method on the `PostgresClient`:
0 commit comments