最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ios - How to call this javascript function from WKWebView Swift? - Stack Overflow

programmeradmin0浏览0评论

So I have this function on my site:

function appQrHandlerSet(result) {
    jQuery(function() {
        jQuery('#readed_qr_url').val(result.url);
        jQuery(this).getLayerForm('#qr_handler_layer');
            });      
}

So I have this function on my site:

function appQrHandlerSet(result) {
    jQuery(function() {
        jQuery('#readed_qr_url').val(result.url);
        jQuery(this).getLayerForm('#qr_handler_layer');
            });      
}

From the iOS app, I have to call this function and pass a JSON to it, how can I achieve that? I've been trying to make it work for 3 days now, but I gave up because something is not working right.

Thanks in advance!

Share Improve this question asked Jul 26, 2018 at 1:16 Barczi BBarczi B 131 silver badge3 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

According to your code, the parameter result should contain the property url. We suppose that the url contains the JSON data you want to pass.
Try the following 2 approaches:

// Approach 1:
func callJS() {
    let json = "{ url:\"An url with json?\"}"
    let scriptString = "let result=\(json); appQrHandlerSet(result);"
    webView?.evaluateJavaScript(scriptString, pletionHandler: { (object, error) in

    })
}

// Approach 2:
func initWebViewWithJs() {
    let config = WKWebViewConfiguration()
    config.userContentController = WKUserContentController()

    let json = "{ url:\"An url with json?\"}"
    let scriptString = "let result=\(json); appQrHandlerSet(result);"
    let script = WKUserScript(source: scriptString, injectionTime: WKUserScriptInjectionTime.atDocumentEnd, forMainFrameOnly: true)
    config.userContentController.addUserScript(script)

    webView = WKWebView(frame: CGRect(x: 0, y: 0, width: 320, height: 400), configuration: config)
}
发布评论

评论列表(0)

  1. 暂无评论