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.

Maila
  • 31
  • 5
  • People won't be able to help without showing what you have done so far. Bits of code snippets would do. – Jay Mayu Jul 31 '18 at 10:21
  • Possible duplicate of [iOS JavaScript bridge](https://stackoverflow.com/questions/9473582/ios-javascript-bridge) – Cristik Jul 31 '18 at 11:50

1 Answers1

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: "")



        }
    }
Nancy Madan
  • 439
  • 4
  • 7
  • I mainly want to send value of one of the variables in javascript to swift . Can you help. – Maila Jul 31 '18 at 10:44
  • Please try the solution above and let me know, Call this from JavaScipt on action :- window.location = "callback://0/"+ pass your value here – Nancy Madan Jul 31 '18 at 10:51
  • instead of using `window.location`, can i simple use ajax? – Maria Oct 16 '18 at 12:56