Return HTML string from the completion handler of the evaluateJavaScript function

落爺英雄遲暮 提交于 2019-12-24 00:38:44

问题


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


回答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) 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)
})


来源:https://stackoverflow.com/questions/45689874/return-html-string-from-the-completion-handler-of-the-evaluatejavascript-functio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!