iOS Quickstart
Before you start
Example repository
Enable Native API
In the Clerk Dashboard, navigate to the Native applications page and ensure that the Native API is enabled. This is required to integrate Clerk in your native application.
Create a new iOS app
If you don't already have an iOS app, create a new project in Xcode. Select SwiftUI as your interface and Swift as your language. See the Xcode documentation for more information.
Install the Clerk iOS SDK
Follow the Swift Package Manager instructions to install Clerk as a dependency.
When prompted for the package URL, enter https://github.com/clerk/clerk-ios. Be sure to add the package to your target.
Add your Native Application
Add your iOS application to the Native applications page in the Clerk Dashboard. You will need your iOS app's App ID Prefix and Bundle ID.
Add associated domain capability
To enable seamless authentication flows, you need to add an associated domain capability to your iOS app. This allows your app to work with Clerk's authentication services.
- In Xcode, select your project in the Project Navigator.
- Select your app target.
- Navigate to the Signing & Capabilities tab.
- Select the + Capability option.
- Search for and add Associated Domains. It will be added as a dropdown to the Signing & Capabilities tab.
- Under Associated Domains, add a new entry with the value:
webcredentials:{YOUR_FRONTEND_API_URL}
Load Clerk
To use Clerk in your app, you must first configure and load Clerk.
- Inside your new project in Xcode, open your
@mainapp file. - Import
Clerk. - Create a reference to the shared
Clerkinstance. - Inject the
clerkinstance into the SwiftUI environment using.environment(\.clerk, clerk)so your views can access it. - Add a
.taskmodifier that configures Clerk with your Clerk and loads it when the app starts.
import SwiftUI
import Clerk
@main
struct ClerkQuickstartApp: App {
@State private var clerk = Clerk.shared
var body: some Scene {
WindowGroup {
ContentView()
.environment(\.clerk, clerk)
.task {
clerk.configure(publishableKey: "YOUR_PUBLISHABLE_KEY")
try? await clerk.load()
}
}
}
}Conditionally render content
To render content based on whether a user is authenticated or not:
- Open your
ContentViewfile. - Import
Clerkand access the sharedClerkinstance that you injected into the environment in the previous step. - Replace the content of the view body with a conditional that checks for a
clerk.user.
import SwiftUI
import Clerk
struct ContentView: View {
@Environment(\.clerk) private var clerk
var body: some View {
VStack {
if let user = clerk.user {
Text("Hello, \(user.firstName ?? "User")")
} else {
Text("You are signed out")
}
}
}
}Use Clerk's prebuilt views
Clerk provides prebuilt SwiftUI views that handle authentication flows and user management, eliminating the need to build custom forms and flows.
Update your ContentView to use two key Clerk's prebuilt views:
-
AuthView: A comprehensive authentication view that handles sign-in and sign-up flows, including email verification, password reset, and multi-factor authentication. It's presented as a sheet when the user taps "Sign in".
-
UserButton: A circular button that displays the user's profile image. When tapped, it automatically presents the UserProfileView where users can manage their account, update their profile, and sign out.
Replace the existing content with the following code:
import SwiftUI
import Clerk
struct ContentView: View {
@Environment(\.clerk) private var clerk
@State private var authIsPresented = false
var body: some View {
VStack {
if clerk.user != nil {
UserButton()
.frame(width: 36, height: 36)
} else {
Button("Sign in") {
authIsPresented = true
}
}
}
.sheet(isPresented: $authIsPresented) {
AuthView()
}
}
}Run your project
In Xcode, select Run ▶︎ to build and launch the app.
Create your first user
Once the app launches successfully, tap "Sign in" and the AuthView will appear, allowing you to sign up or sign in to create your first user.
Next steps
Learn more about Clerk views, how to customize them, and explore the Clerk iOS SDK using the following guides.
Clerk iOS SDK documentation
Learn more about the iOS SDK.
Clerk views
Learn more about Clerk's suite of views that let you quickly add authentication to your app.
Customization with ClerkTheme
Learn how to customize Clerk views using ClerkTheme.
Feedback
Last updated on