-
Notifications
You must be signed in to change notification settings - Fork 22
/
ContentView.swift
52 lines (46 loc) · 1.36 KB
/
ContentView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//
// ContentView.swift
// HCaptcha_PassiveExample
//
// Copyright © 2024 HCaptcha. MIT License.
//
import SwiftUI
import HCaptcha
struct ContentView: View {
@State private var token: String?
@State private var isLoading = false
var body: some View {
VStack {
if isLoading {
ProgressView()
} else {
Text(token ?? "no token")
}
Button("Validate", action: {
isLoading = true
Task {
token = await fetchHCaptchaToken()
isLoading = false
}
}).padding()
}
}
func fetchHCaptchaToken() async -> String? {
guard let hcaptcha = try? HCaptcha(
apiKey: "10000000-ffff-ffff-ffff-000000000001",
passiveApiKey: true,
baseURL: URL(string: "http://localhost")!,
diagnosticLog: true
) else { return "init error" }
return await withCheckedContinuation { continuation in
hcaptcha.validate { result in
do {
let result = try result.dematerialize()
continuation.resume(returning: result)
} catch let error {
continuation.resume(returning: error.localizedDescription)
}
}
}
}
}