iOS SDK
Native Swift SDK for iOS applications using AppAuth for secure OAuth 2.0 PKCE authentication with Keychain storage.
Features
- ✅ OAuth 2.0 PKCE flow via AppAuth
- ✅ Secure Keychain token storage
- ✅ Swift async/await support
- ✅ Automatic token refresh
- ✅ License and entitlement checking
- ✅ SwiftUI compatible
Installation
Swift Package Manager
Add to your Package.swift:
dependencies: [
.package(url: "https://github.com/optare/optareid-ios.git", from: "0.1.0")
]Or in Xcode: File → Add Packages and enter the repository URL.
CocoaPods
pod 'OptareAuth', '~> 0.1.0'Configuration
1. Configure URL Scheme
Add to your Info.plist:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>myapp</string>
</array>
</dict>
</array>2. Initialize SDK
import OptareAuth
@main
struct MyApp: App {
init() {
OptareAuth.configure(
clientId: "your-client-id",
redirectUri: URL(string: "myapp://callback")!
)
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
}3. Handle URL Callback
// In your App or SceneDelegate
.onOpenURL { url in
OptareAuth.handleURL(url)
}Usage
Login
import OptareAuth
struct LoginView: View {
@State private var user: OptareUser?
var body: some View {
Button("Login with Optare") {
Task {
do {
user = try await OptareAuth.login(from: viewController)
print("Logged in as \(user?.name ?? "Unknown")")
} catch {
print("Login failed: \(error)")
}
}
}
}
}Check Authentication Status
if OptareAuth.isAuthenticated {
print("User is logged in")
print("Current user: \(OptareAuth.currentUser?.name ?? "Unknown")")
}Get Access Token
// Automatically refreshes if expired
let token = try await OptareAuth.getAccessToken()Logout
await OptareAuth.logout()License Checking
// Check if user has a product license
if OptareAuth.hasLicense("premium-plan") {
// Show premium features
}
// Check for specific entitlements
if OptareAuth.hasEntitlement("ai-access") {
// Enable AI features
}OptareUser Properties
public struct OptareUser {
let id: String // Unique user identifier
let email: String? // Email address
let emailVerified: Bool? // Email verification status
let name: String? // Display name
let image: String? // Profile picture URL
let organizationId: String? // Organization ID
let licenses: [String] // Product licenses
let entitlements: [String] // Feature entitlements
let roles: [String] // User roles
}Error Handling
do {
let user = try await OptareAuth.login(from: vc)
} catch OptareError.notConfigured {
print("SDK not configured")
} catch OptareError.authorizationFailed(let message) {
print("Auth failed: \(message)")
} catch OptareError.tokenError(let message) {
print("Token error: \(message)")
} catch {
print("Unknown error: \(error)")
}Requirements
- iOS 14.0+
- Swift 5.5+
- Xcode 13+
Dependencies
- AppAuth-iOS (opens in a new tab) - OAuth 2.0 / OIDC
- KeychainAccess (opens in a new tab) - Secure storage
See GitHub Repository (opens in a new tab) for full source code.