Skip to content

Commit

Permalink
Bugfix: allow calling verify in didLoad / didFinishLoading state (#133)
Browse files Browse the repository at this point in the history
* feat: add threadId to HCaptchaLog statements

* fix: prevent double execute calls on validation from didFinishLoading

* chore: update header in HCaptchaHtml

* fix: broken executeJS logic after rearange didFinishLoading=true

* chore: bump to 2.5.2
  • Loading branch information
CAMOBAP authored Feb 10, 2024
1 parent afc40cc commit a62aa18
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 2.5.2

- Fix: disabled controls on visual challenge if validation requested from didFinishLoading

# 2.5.1

- Feature: new `diagnosticLog` argument to enable debug logs.
Expand Down
17 changes: 17 additions & 0 deletions Example/HCaptcha_Tests/Core/HCaptcha__Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,23 @@ class HCaptcha__Tests: XCTestCase {
XCTFail("Unexpected error: \(e)")
}
}

func test__validate_from_didFinishLoading() {
let exp = expectation(description: "execute js function must be called only once")
let hcaptcha = HCaptcha(manager: HCaptchaWebViewManager(messageBody: "{action: \"showHCaptcha\"}"))
hcaptcha.didFinishLoading {
let view = UIApplication.shared.windows.first?.rootViewController?.view
hcaptcha.onEvent { e, _ in
if e == .open {
exp.fulfill()
}
}
hcaptcha.validate(on: view!) { _ in
XCTFail("Should not be called")
}
}
wait(for: [exp], timeout: 10)
}
}


Expand Down
8 changes: 4 additions & 4 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PODS:
- AppSwizzle (1.3.1)
- HCaptcha/Core (2.5.1)
- HCaptcha/RxSwift (2.5.1):
- HCaptcha/Core (2.5.2)
- HCaptcha/RxSwift (2.5.2):
- HCaptcha/Core
- RxSwift (~> 6.2.0)
- RxBlocking (6.2.0):
Expand Down Expand Up @@ -37,7 +37,7 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
AppSwizzle: db36e436f56110d93e5ae0147683435df593cabc
HCaptcha: 98dc7a73b11b5dee7e6fced7085925a9c74d9ca4
HCaptcha: 300b41b7b02e96b1373d07f011461698f9e0c65e
RxBlocking: 0b29f7d2079109a8de49c411381bed7c33ef1eeb
RxCocoa: 4baf94bb35f2c0ab31bc0cb9f1900155f646ba42
RxRelay: e72dbfd157807478401ef1982e1c61c945c94b2f
Expand All @@ -46,4 +46,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 44657674683c7639f916ef6eb6fae5387395d652

COCOAPODS: 1.12.0
COCOAPODS: 1.15.1
2 changes: 1 addition & 1 deletion HCaptcha.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'HCaptcha'
s.version = '2.5.1'
s.version = '2.5.2'
s.summary = 'HCaptcha for iOS'
s.swift_version = '5.0'

Expand Down
2 changes: 1 addition & 1 deletion HCaptcha/Classes/HCaptchaHtml.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// HCaptchaHtml.swift (Autogenerated)
// HCaptcha
//
// Copyright © 2023 HCaptcha. All rights reserved.
// Copyright © 2024 HCaptcha. All rights reserved.
//

import Foundation
Expand Down
6 changes: 5 additions & 1 deletion HCaptcha/Classes/HCaptchaLog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal class HCaptchaLogger {
}

let formattedMessage = String(format: message, arguments: args)
let logMessage = "\(timestamp) HCaptcha/\(level.description): \(formattedMessage)"
let logMessage = "\(timestamp) \(threadId) HCaptcha/\(level.description): \(formattedMessage)"

print(logMessage)
#endif
Expand All @@ -61,4 +61,8 @@ internal class HCaptchaLogger {
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.sss"
return dateFormatter.string(from: Date())
}

private static var threadId: String {
return Thread.isMainThread ? "main" : "\(pthread_self())"
}
}
13 changes: 9 additions & 4 deletions HCaptcha/Classes/HCaptchaWebViewManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,10 @@ fileprivate extension HCaptchaWebViewManager {

private func didLoad() {
Log.debug("WebViewManager.didLoad")
didFinishLoading = true
if completion != nil {
executeJS(command: .execute)
executeJS(command: .execute, didLoad: true)
}
didFinishLoading = true
self.doConfigureWebView()
}

Expand Down Expand Up @@ -326,13 +326,14 @@ fileprivate extension HCaptchaWebViewManager {
/**
- parameters:
- command: The JavaScript command to be executed
- didLoad: True if didLoad event already occured

Executes the JS command that loads the HCaptcha challenge. This method has no effect if the webview hasn't
finished loading.
*/
func executeJS(command: JSCommand) {
func executeJS(command: JSCommand, didLoad: Bool = false) {
Log.debug("WebViewManager.executeJS: \(command)")
guard didFinishLoading else {
guard didLoad else {
if let error = lastError {
DispatchQueue.main.async { [weak self] in
Log.debug("WebViewManager complete with pendingError: \(error)")
Expand All @@ -352,6 +353,10 @@ fileprivate extension HCaptchaWebViewManager {
}
}
}

func executeJS(command: JSCommand) {
executeJS(command: command, didLoad: self.didFinishLoading)
}
}

extension HCaptchaWebViewManager: WKNavigationDelegate {
Expand Down

0 comments on commit a62aa18

Please sign in to comment.