Supabase client for swift. Mirrors the design of supabase-js.
Swift Package Manager:
Add the following lines to your Package.swift
file:
let package = Package(
...
dependencies: [
...
.package(name: "Supabase", url: "https://github.com/supabase/supabase-swift.git", branch: "master"), // Add the package
],
targets: [
.target(
name: "YourTargetName",
dependencies: ["Supabase"] // Add as a dependency
)
]
)
If you're using Xcode, use this guide to add supabase-swift
to your project. Use https://github.com/supabase/supabase-swift.git
for the url when Xcode asks.
To make requests to the Supabase
database, you will need to initialize a SupabaseClient
object:
let client = SupabaseClient(supabaseURL: "{ Supabase URL }", supabaseKey: "{ Supabase anonymous Key }")
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.
Session
info:Task {
do {
try await client.auth.signUp(email: email, password: password)
let session = try await client.auth.session
print("### Session Info: \(session)")
} catch {
print("### Sign Up Error: \(error)")
}
}
Session
info:Task {
do {
try await client.auth.signIn(email: email, password: password)
let session = try await client.auth.session
print("### Session Info: \(session)")
} catch {
print("### Sign Up Error: \(error)")
}
}
We need to first set up the callback URL for all Social Logins inside the app.
URL
on Info.plist
:<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>app.yourapp://login-callback</string>
</array>
</dict>
</array>
URL
on Supabase
under Authentication -> URL Configuration -> Redirect URLs
.SafariServices
in your ViewController
and create a SFSafariViewController
instance:import SafariServices
var safariVC: SFSafariViewController?
URL
for Google Sign in from Supabase
and load it on SFSafariViewController
.URL
you set up in the redirecTo
parameter:Task {
do {
let url = try await client.auth.getOAuthSignInURL(provider: Provider.google, redirectTo: URL(string: {Your Callback URL})!)
safariVC = SFSafariViewController(url: url as URL)
self.present(safariVC!, animated: true, completion: nil)
} catch {
print("### Google Sign in Error: \(error)")
}
}
URL
on SceneDelegate
(for older projects, you can use AppDelegate
if SceneDelegate
is not present).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.func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
if let url = URLContexts.first?.url as? URL {
if url.host == "login-callback" {
let urlDict: [String: URL] = ["url": url]
NotificationCenter.default.post(name: Notification.Name("OAuthCallBack"), object: nil, userInfo: urlDict)
}
}
}
ViewController
, observe for the Notification
and handle it minimizing the SFSafariViewController
and getting the session:NotificationCenter.default.addObserver(
self,
selector: #selector(self.oAuthCallback(_:)),
name: NSNotification.Name(rawValue: "OAuthCallBack"),
object: nil)
@objc func oAuthCallback(_ notification: NSNotification){
guard let url = notification.userInfo?["url"] as? URL else { return }
Task {
do {
let session = try await SupaBaseAuth().client.session(from: url)
print("### Session Info: \(session)")
} catch {
print("### oAuthCallback error: \(error)")
}
}
safariVC?.dismiss(animated: true)
}
SFSafariViewController
, an Apple native pop-up will slide up to continue with the sign in.Task {
do {
let url = try await client.auth.getOAuthSignInURL(provider: **Provider.apple**, redirectTo: URL(string: {Your Callback URL})!)
safariVC = SFSafariViewController(url: url as URL)
self.present(safariVC!, animated: true, completion: nil)
} catch {
print("### Apple Sign in Error: \(error)")
}
}
First, import and initialize SupabaseClient
, as explained in "Usage" section.
struct InsertModel: Encodable, Decodable {
let id: Int? // you can choose to omit this depending on how you've setup your table
let title: String?
let desc: String?
}
let insertData = InsertModel(title: "Test", desc: "Test Desc")
let query = client.database
.from("{ Your Table Name }")
.insert(values: insertData,
returning: .representation) // you will need to add this to return the added data
.select(columns: "id") // specifiy which column names to be returned. Leave it empty for all columns
.single() // specify you want to return a single value.
Task {
do {
let response: [InsertModel] = try await query.execute().value
print("### Returned: \(response)")
} catch {
print("### Insert Error: \(error)")
}
}
let insertData = InsertModel(title: "Test", desc: "Test Desc")
let query = client.database
.from("{ Your Table Name }")
.select() // keep it empty for all, else specify returned data
.match(query: ["title" : insertData.title, "desc": insertData.desc])
.single()
Task {
do {
let response: [InsertModel] = try await query.execute().value
print("### Returned: \(response)")
} catch {
print("### Insert Error: \(error)")
}
}
We are building the features of Firebase using enterprise-grade, open source products. We support existing communities wherever possible, and if the products don’t exist we build them and open source them ourselves. Thanks to these sponsors who are making the OSS ecosystem better for everyone.
link |
Stars: 260 |
Last commit: 1 week ago |
Full Changelog: https://github.com/supabase-community/supabase-swift/compare/0.2.0...0.2.1
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics