forked from hCaptcha/HCaptcha-ios-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewController.swift
190 lines (160 loc) · 6.05 KB
/
ViewController.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//
// ViewController.swift
// HCaptcha
//
// Created by Flávio Caetano on 03/22/17.
// Copyright © 2018 HCaptcha. All rights reserved.
//
import HCaptcha
import RxCocoa
import RxSwift
import UIKit
class ViewController: UIViewController {
private struct Constants {
static let webViewTag = 123
static let testLabelTag = 321
}
private var hcaptcha: HCaptcha!
private var disposeBag = DisposeBag()
private var locale: Locale?
/// Don't init SDK to avoid unnecessary API calls and simplify debugging if the application used as a host for tests
private var unitTesting: Bool {
return ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"] != nil
}
@IBOutlet private weak var label: UILabel!
@IBOutlet private weak var spinner: UIActivityIndicatorView!
@IBOutlet private weak var localeSegmentedControl: UISegmentedControl!
@IBOutlet private weak var apiSegmentedControl: UISegmentedControl!
@IBOutlet private weak var visibleChallengeSwitch: UISwitch!
@IBOutlet private weak var validateButton: UIButton!
@IBOutlet private weak var resetButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
setupHCaptcha()
}
@IBAction func didPressLocaleSegmentedControl(_ sender: UISegmentedControl) {
label.text = ""
switch sender.selectedSegmentIndex {
case 0: locale = nil
case 1: locale = Locale(identifier: "zh-CN")
default: assertionFailure("invalid index")
}
setupHCaptcha()
}
@IBAction private func didPressVerifyButton(button: UIButton) {
if self.apiSegmentedControl.selectedSegmentIndex == 0 {
self.verifyRxApi()
} else {
self.verifyRegularApi()
}
}
private func verifyRegularApi() {
hcaptcha.validate(on: self.view) { result in
do {
self.label.text = try result.dematerialize()
} catch let error as HCaptchaError {
self.label.text = error.description
} catch let error {
self.label.text = String(describing: error)
}
let subview = self.view.viewWithTag(Constants.webViewTag)
subview?.removeFromSuperview()
}
}
// swiftlint:disable function_body_length
private func verifyRxApi() {
disposeBag = DisposeBag()
hcaptcha.rx.didFinishLoading
.debug("did finish loading")
.subscribe()
.disposed(by: disposeBag)
hcaptcha.rx.events()
.debug("events")
.subscribe { [weak self] (event, data) in
if event == .error {
let error = data as? HCaptchaError
print("onEvent error: \(String(describing: error))")
}
let alertController = UIAlertController(title: "On Event",
message: event.rawValue,
preferredStyle: .alert)
self?.present(alertController, animated: true) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
alertController.dismiss(animated: true, completion: nil)
}
}
}
.disposed(by: disposeBag)
let validate = hcaptcha.rx.validate(on: view, resetOnError: false)
.catch { error in
return .just("Error \(error)")
}
.debug("validate")
.share()
let isLoading = validate
.map { _ in false }
.startWith(true)
.share(replay: 1)
isLoading
.bind(to: spinner.rx.isAnimating)
.disposed(by: disposeBag)
let isEnabled = isLoading
.map { !$0 }
.catchAndReturn(false)
.share(replay: 1)
isEnabled
.bind(to: validateButton.rx.isEnabled)
.disposed(by: disposeBag)
validate
.map { [weak self] _ in
self?.view.viewWithTag(Constants.webViewTag)
}
.subscribe(onNext: { subview in
subview?.removeFromSuperview()
})
.disposed(by: disposeBag)
validate
.bind(to: label.rx.text)
.disposed(by: disposeBag)
visibleChallengeSwitch.rx.value
.subscribe(onNext: { [weak hcaptcha] value in
hcaptcha?.forceVisibleChallenge = value
})
.disposed(by: disposeBag)
resetButton.rx.tap
.subscribe(onNext: { [weak hcaptcha] _ in
hcaptcha?.reset()
})
.disposed(by: disposeBag)
}
@IBAction private func didPressStopButton(button: UIButton) {
hcaptcha.stop()
}
@IBAction private func didPressResetButton(button: UIButton) {
hcaptcha?.reset()
}
private func setupHCaptcha() {
if unitTesting {
return
}
// swiftlint:disable:next force_try
hcaptcha = try! HCaptcha(locale: locale)
hcaptcha.configureWebView { [weak self] webview in
webview.frame = self?.view.bounds ?? CGRect.zero
webview.tag = Constants.webViewTag
// could use this option if using an enterprise passive sitekey:
// webview.isHidden = true
// seems to prevent flickering on latest iOS 15.2
webview.isOpaque = false
webview.backgroundColor = UIColor.clear
webview.scrollView.backgroundColor = UIColor.clear
// For testing purposes
// If the webview requires presentation, this should work as a way of detecting the webview in UI tests
self?.view.viewWithTag(Constants.testLabelTag)?.removeFromSuperview()
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
label.tag = Constants.testLabelTag
label.accessibilityLabel = "webview"
self?.view.addSubview(label)
}
}
}