SDK
Swift SDK
AuthonSwift — ネイティブSign in with Apple、OAuth向けASWebAuthenticationSession、KeychainトークンストレージのiOS/macOS SDK。
インストール
XcodeのSwift Package Managerで追加:
Package.swift
dependencies: [
.package(url: "https://github.com/mikusnuz/authon-swift", from: "1.0.0")
]セットアップ
AuthonApp.swift
import SwiftUI
import AuthonSwift
@main
struct AuthonApp: App {
init() {
Authon.configure(
publishableKey: "pk_live_your_key",
redirectScheme: "myapp"
)
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
}SwiftUI使い方
ContentView.swift
import SwiftUI
import AuthonSwift
struct ContentView: View {
@StateObject private var auth = AuthonState.shared
var body: some View {
Group {
if auth.isLoading {
ProgressView()
} else if auth.isSignedIn {
VStack {
Text("Welcome, \(auth.user?.displayName ?? "")!")
Button("Sign out") {
Task { await Authon.shared.signOut() }
}
}
} else {
Button("Sign in") {
Task { await Authon.shared.openSignIn() }
}
}
}
}
}OAuthサインイン
swift
// Sign in with Google via ASWebAuthenticationSession
try await Authon.shared.signInWithOAuth(provider: "google")
// Sign in with Apple (native)
try await Authon.shared.signInWithApple()