Stytch offers a comprehensive mobile authentication solution that simplifies integration with its API using our mobile SDKs. As the only authentication provider with a complete set of APIs, Stytch enables the creation of custom end-to-end authentication flows tailored to your mobile tech stack. With two integration options, Stytch
and StytchUI
, Stytch's SDKs allow you to craft an authentication experience that flexibility integrates into your app. Stytch
offers a fully customizable headless API integration to suit your specific needs, while StytchUI
provides a configurable view to expedite the integration process.
Note: Currently StytchUI only supports our consumer client, B2B UI coming soon!
If you are completely new to Stytch, prior to using the SDK you will first need to visit Stytch's homepage, sign up, and create a new project in the dashboard. You'll then need to adjust your SDK configuration — adding your app's bundle id to Authorized applications
and enabling any Auth methods
you wish to use.
Stytch uses the Swift Package Manager for managing the distribution of our Swift code. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies. To add the Stytch SDK to your Xcode project complete the following steps.
- Open Xcode
- File > Add Package Dependencies
- Enter https://github.com/stytchauth/stytch-ios in the top right
- Choose Package Requirements and click "Add Package"
- In your Build Settings, under
Other Linker Flags
, add-ObjC
import StytchCore
in your code
We highly recommend that you use "Up To Next Major Version" and never point to main
or any other branch directly. Knowing what version of the SDK you are using will make it easier for us to support you!
Before using any part of the Stytch SDK, you must call configure to set the public token as specified in your project dashboard.
import StytchCore
StytchClient.configure(publicToken: "your-public-token")
StytchCore exposes clients for Consumer and B2B, make sure to use the one that corresponds with your project configuration. For the sake of the following examples we will be using the consumer one: StytchClient.
The following basic example is for using OTP (One Time Passcode) to authenticate.
import StytchCore
public class OTPAuthenticationManager {
var methodId: String = ""
// Send a OTP (one time passcode) via SMS
func sendOTP() {
Task {
do {
let parameters = StytchClient.OTP.Parameters(deliveryMethod: .sms(phoneNumber: "+12125551234"))
let response = try await StytchClient.otps.send(parameters: parameters)
// save the methodId for the subsequent authenticate call
self.methodId = response.methodId
} catch {
print(error.errorInfo)
}
}
}
// Authenticate a user using the OTP sent via SMS
func authenticateOTP(code: String) {
Task {
do {
let parameters = StytchClient.OTP.AuthenticateParameters(code: code, methodId: methodId)
let response = try await StytchClient.otps.authenticate(parameters: parameters)
print(response.user)
} catch {
print(error.errorInfo)
}
}
}
}
Whereas StytchCore
is written using async/await
, we use Sourcery to generate versions of the API that can be used with Combine
or called with a completion handler. If you look in the generated directory you will see the APIs with files ending in +AsyncVariants
that hold the generated code. For instance, the async variants for the above APIs can be referenced here: authenticate & send.
For further information and tutorials on some of our more common implementations, see the following:
Full reference documentation is available for StytchCore and StytchUI.
Instructions can be found here!
Join the discussion, ask questions, and suggest new features in our Slack community!
Check out the Stytch Forum or email us at [email protected].
The Stytch iOS SDK is released under the MIT license. See LICENSE for details.