|
1 |
| -# `supabase-swift` |
| 1 | +# supabase-swift |
2 | 2 |
|
3 |
| -Swift Client library to interact with Supabase. |
| 3 | +Supabase client for swift. Mirrors the design of [supabase-js](https://github.com/supabase/supabase-js/blob/master/README.md) |
4 | 4 |
|
5 |
| -- Documentation: https://supabase.io/docs/reference/swift/supabase-client |
| 5 | +## Installation |
| 6 | + |
| 7 | +Swift Package Manager: |
| 8 | + |
| 9 | +Add the following lines to your `Package.swift` file: |
| 10 | +```swift |
| 11 | +let package = Package( |
| 12 | + ... |
| 13 | + dependencies: [ |
| 14 | + ... |
| 15 | + .package(name: "Supabase", url: "https://github.com/supabase/supabase-swift.git", .branch("main")), // Add the package |
| 16 | + ], |
| 17 | + targets: [ |
| 18 | + .target( |
| 19 | + name: "YourTargetName", |
| 20 | + dependencies: ["Supabase"] // Add as a dependency |
| 21 | + ) |
| 22 | + ] |
| 23 | +) |
| 24 | +``` |
| 25 | + |
| 26 | +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. |
| 27 | + |
| 28 | +## Usage |
| 29 | + |
| 30 | +For all requests made for supabase, you will need to initialize a `SupabaseClient` object. |
| 31 | +```swift |
| 32 | +let client = SupabaseClient(supabaseUrl: "{ Supabase URL }", supabaseKey: "{ Supabase anonymous Key }") |
| 33 | +``` |
| 34 | +This client object will be used for all the following examples. |
| 35 | + |
| 36 | +### Database |
| 37 | + |
| 38 | +Query todo table for all completed todos. |
| 39 | +```swift |
| 40 | +do { |
| 41 | + let query = try client.database.from("todos") |
| 42 | + .select() |
| 43 | + .eq(column: "isDone", value: "true") |
| 44 | + |
| 45 | + try query.execute { [weak self] (results) in |
| 46 | + guard let self = self else { return } |
| 47 | + |
| 48 | + // Handle results |
| 49 | + } |
| 50 | +} catch { |
| 51 | + print("Error querying for todos: \(error)") |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +Insert a todo into the database. |
| 56 | +```swift |
| 57 | +struct Todo: Codable { |
| 58 | + var id: UUID = UUID() |
| 59 | + var label: String |
| 60 | + var isDone: Bool = false |
| 61 | +} |
| 62 | + |
| 63 | +let todo = Todo(label: "Example todo!") |
| 64 | + |
| 65 | +do { |
| 66 | + let jsonData: Data = try JSONEncoder().encode(todo) |
| 67 | + let jsonDict: [String: Any] = try JSONSerialization.jsonObject(with: jsonData, options: .allowFragments)) |
| 68 | + |
| 69 | + try client.database.from("todos") |
| 70 | + .insert(values: jsonDict) |
| 71 | + .execute { results in |
| 72 | + // Handle response |
| 73 | + } |
| 74 | +} catch { |
| 75 | + print("Error inserting the todo: \(error)") |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +For more query examples visit [the Javascript docs](https://supabase.io/docs/reference/javascript/select) to learn more. The API design is a near 1:1 match. |
| 80 | + |
| 81 | +Execute an RPC |
| 82 | +```swift |
| 83 | +do { |
| 84 | + try client.database.rpc(fn: "testFunction", parameters: nil).execute { result in |
| 85 | + // Handle result |
| 86 | + } |
| 87 | +} catch { |
| 88 | + print("Error executing the RPC: \(error)") |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +### Realtime |
| 93 | + |
| 94 | +> Realtime docs coming soon |
| 95 | +
|
| 96 | +### Auth |
| 97 | + |
| 98 | +> Auth docs coming soon |
| 99 | +
|
| 100 | +### Storage |
| 101 | + |
| 102 | +> Storage docs coming soon |
| 103 | +
|
| 104 | + |
| 105 | +## Contributing |
| 106 | + |
| 107 | +- Fork the repo on GitHub |
| 108 | +- Clone the project to your own machine |
| 109 | +- Commit changes to your own branch |
| 110 | +- Push your work back up to your fork |
| 111 | +- Submit a Pull request so that we can review your changes and merge |
6 | 112 |
|
7 | 113 | ## Sponsors
|
8 | 114 |
|
|
0 commit comments