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 p
Solution 1:
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) iniflet 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)
})
Post a Comment for "Return Html String From The Completion Handler Of The Evaluatejavascript Function"