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 ()
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)
})