0

I am injecting a script in UIWebView using Swift. When a certain tap action happens, the script takes care of the handling. While this happens, the JavaScript is supposed to make a call to one of the functions in Swift, which saves the data sent from JavaScript in native variable. Please help me in making this call.

2
  • People won't be able to help without showing what you have done so far. Bits of code snippets would do. Commented Jul 31, 2018 at 10:21
  • Possible duplicate of iOS JavaScript bridge Commented Jul 31, 2018 at 11:50

1 Answer 1

2

For this you need to make a callback from your JavaScript file, I used one JavaScript editor in that,This was the code :-

window.location = "callback://0/"+ pass your value here

And in your swift file, use the UIWebViewDelegate :-

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        let urlString = request.url?.absoluteString
        //NSLog(@"web request");
        //NSLog(@"%@", urlString);
        if ((urlString?.range(of: "callback://0/")) != nil) {

            // We recieved the callback
            let value = urlString?.replacingOccurrences(of: "callback://0/", with: "")



        }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

I mainly want to send value of one of the variables in javascript to swift . Can you help.
Please try the solution above and let me know, Call this from JavaScipt on action :- window.location = "callback://0/"+ pass your value here
instead of using window.location, can i simple use ajax?

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.