1

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!

1 Answer 1

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, completionHandler: { (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)
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, the first approach solved all my problems!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.