how to call a swift function from javascript

Multi tool use


how to call a swift function from javascript
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.
UIWebView
1 Answer
1
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: "")
}
}
I mainly want to send value of one of the variables in javascript to swift . Can you help.
– Maila
3 mins ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
People won't be able to help without showing what you have done so far. Bits of code snippets would do.
– Jay Mayu
27 mins ago