Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return HTML string from the completion handler of the evaluateJavaScript function

I know that I'm not the first one to ask this but I can't solve the problem. I'm trying to take a piece of string from HTML using evaluateJavaScript in Xcode with Swift 3 and the piece of text is called value inside the completion handler, so I did like this:

var userName = String()

   func takeData() {
        webView.evaluateJavaScript("document.querySelectorAll('.name')[0].innerHTML") { (value, error) in

            if let valueName = value as? String {
                self.userName = valueName
            }
            print(value)
            print(error)
        }

    }

print(" The name is : \(self.userName)") 

The problem is that the console just prints: The name is ()

like image 880
Albifer Avatar asked Dec 31 '25 17:12

Albifer


1 Answers

The problem is that you are printing the value before your asynchronous function could finish execution. You have several solutions to solve this issue. You can either implement takeData to have a completionHandler as one of its input parameters, use GCD to make your statements execute in the expected order or use a 3rd party library, such as PromiseKit to handle the async requests for you, so they will behave like normal functions with a return value.

I will give you an example with the completion handler:

func takeData(completionHandler: @escaping (_ userName: String?) -> Void){
    webView.evaluateJavaScript("document.querySelectorAll('.name')[0].innerHTML") { (value, error) in
        if let valueName = value as? String {
            completionHandler(valueName)
        }
        print(value)
        print(error)
        completionHandler(nil)
    }
}

You use the value from the completionHandler like this:

takeData(completionHandler: { userName in
    print(userName)
})
like image 136
Dávid Pásztor Avatar answered Jan 03 '26 05:01

Dávid Pásztor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!