Repository navigation
Expand file tree
/
Copy pathContentView.swift
More file actions
65 lines (55 loc) · 1.52 KB
/
Copy pathContentView.swift
File metadata and controls
65 lines (55 loc) · 1.52 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
//
// ContentView.swift
// HCaptcha_SwiftUI_Example
//
// Copyright © 2024 HCaptcha. MIT License.
//
import SwiftUI
import HCaptcha
// Wrapper-view to provide UIView instance
struct UIViewWrapperView: UIViewRepresentable {
var uiView = UIView()
func makeUIView(context: Context) -> UIView {
uiView.backgroundColor = .gray
return uiView
}
func updateUIView(_ view: UIView, context: Context) {
// nothing to update
}
}
class HCaptchaViewModel: ObservableObject {
let hcaptcha: HCaptcha!
init() {
self.hcaptcha = try? HCaptcha()
}
func configure(_ hostView: UIViewWrapperView) {
hcaptcha.configureWebView { webview in
webview.frame = hostView.uiView.bounds
}
hcaptcha.onEvent { (event, _) in
print("HCaptcha onEvent \(event.rawValue)")
}
}
func validate(_ hostView: UIViewWrapperView) {
hcaptcha.validate(on: hostView.uiView) { result in
print("HCaptcha result \(try? result.dematerialize())")
}
}
}
// Example of hCaptcha usage
struct HCaptchaView: View {
@StateObject var model = HCaptchaViewModel()
let placeholder = UIViewWrapperView()
var body: some View {
VStack{
placeholder.frame(width: 640, height: 640, alignment: .center)
Button(
"validate",
action: { model.validate(placeholder) }
).padding()
}
.onAppear {
model.configure(placeholder)
}
}
}