Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hiding recaptcha webview after completing the challenge? #13

Closed
ebeem opened this issue Nov 26, 2017 · 10 comments
Closed

hiding recaptcha webview after completing the challenge? #13

ebeem opened this issue Nov 26, 2017 · 10 comments
Labels

Comments

@ebeem
Copy link

ebeem commented Nov 26, 2017

I am having some problems figuring out how can I hide the webview after displaying the challenge, my code is

recaptcha.validate(on: view) { [weak self] result in
       //some logic here (no recaptcha api calls)
}

after completing the challenge, the puzzle itself disappears, but a blank white screen is all that's left, do I have to hide the webview somehow after completing the challenge?

thanks for the great effort you put in this library

@fjcaetano
Copy link
Owner

Hi @ebeem, thanks for your support.

The webview won't be removed after completion. It's up to you to hide it or to remove it from the superview.

@ebeem
Copy link
Author

ebeem commented Nov 27, 2017

thanks for you quick response @fjcaetano

my use case has multiple actions that can be performed in the same view controller, some of these action require showing the recaptcha, I followed your advise and tried several tricks to hide/remove the webview (from the library code itself, and from my code), I ended up having a recaptchaView that covers the whole screen and is empty and hidden, its purpose would be just to contain the webview of the recaptcha. Here is my code

        recaptchaView.isHidden = false
        recaptcha.configureWebView { [weak self] webview in
            webview.frame = self?.recaptchaView.bounds ?? CGRect.zero
        }
        recaptcha.validate(on: recaptchaView) { [weak self] result in
            for view in (self?.recaptchaView.subviews)! {
                view.removeFromSuperview()
            }
            self?.recaptchaView.isHidden = true

            //my logic here
        }

but no matter what approach I follow, the first attempt of the recaptcha is always successful, shows the recaptcha as expected and sometimes even pass it immediately (invisible recaptcha). However, the second attempt always shows the recaptchaView empty with no webview or recaptcha. Could you please show me what's wrong with my approach?

thanks for your time.

@fjcaetano
Copy link
Owner

I think the library is not cleaned up after use, which means you wouldn't be able to call it twice, since the form has already been sent.

Why do you need to call it twice anyway? If that's really necessary, I think the best way would be to instantiate a new ReCaptcha object.

@ebeem
Copy link
Author

ebeem commented Nov 27, 2017

@fjcaetano thanks for your reply
actually initiating the object again works, but causes crashes sometimes
I will investigate more and keep you updated

thanks for your help

@fjcaetano
Copy link
Owner

No problem. Let me know about your findings

@ebeem
Copy link
Author

ebeem commented Nov 30, 2017

@fjcaetano until now, everything seems to be working perfectly as long as this approach is followed

on initialization, declaring the variable
private var recaptcha: ReCaptcha? = try! ReCaptcha()

I have a recaptchaView that I hide and show according to whether I want to show the puzzle or not (container), and this is how I handle my actions that need bot detection

        recaptchaView.isHidden = false
        recaptcha!.configureWebView { [weak self] webview in
            webview.frame = self?.recaptchaView.bounds ?? CGRect.zero
        }
        
        recaptcha!.validate(on: recaptchaView) { [weak self] result in
            for view in (self?.recaptchaView.subviews)! {
                view.removeFromSuperview()
            }
            
            self?.recaptchaView.isHidden = true
            
            //my logic here

            self?.recaptcha = try! ReCaptcha()
        }

re-initializing the recaptcha before every action instead of at the end of each causes crashes sometimes, probably the recaptcha isn't complete by that time, and thus, a javascript exception occurs.

thanks for your help and the amazing library @fjcaetano , I will close this

@ebeem ebeem closed this as completed Nov 30, 2017
@fjcaetano
Copy link
Owner

Reseting the stored recaptcha value from within the validate closure does seem a bit odd because it'd be freeing the previous value before its closure is completed.

Is the crash you're receiving an exception thrown by the library? Feel free to open another issue with it if you think it's worth it.

@ebeem
Copy link
Author

ebeem commented Nov 30, 2017

it looked exactly just like issue #7
I think it's reproducible, and causes an exception most of the time

        recaptcha: ReCaptcha? = try! ReCaptcha()
        recaptcha!.configureWebView { [weak self] webview in
            webview.frame = self?.recaptchaView.bounds ?? CGRect.zero
        }
        
        recaptcha!.validate(on: recaptchaView) { [weak self] result in
            let token = try! result.dematerialize()
            //my logic here
        }

sadly, the guy who open issue #7 did not provide full code to conclude whether his issue was caused because of late initialization of the recaptcha or not

also, the line that cases the exception is
let token = try! result.dematerialize()

@fjcaetano
Copy link
Owner

Since that's a JS error, it is expected for it to break on dematerialize because the lib is capturing the error and sending it in the result.

Nonetheless, there are verifications to start validation only after the requests have finished (since the lib is actually loading the ReCaptcha scripts), but it seems there may be some bug in that verification, since it doesn't fail when the gap between initialisation and validation is longer.

I may look further into this. Just to be sure, the lib is currently working for you, right?

@ebeem
Copy link
Author

ebeem commented Nov 30, 2017

yup, it works perfectly and totally satisfies my use case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants