Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
167
rated 0 times [  174] [ 7]  / answers: 1 / hits: 23942  / 8 Years ago, wed, november 23, 2016, 12:00:00

I am trying to make a call from a javascript function in a UIWebView to Swift in iOS 10. I have setup a very basic project just to try and get this working, the code is below.



import UIKit

class ViewController: UIViewController, UIWebViewDelegate {
@IBOutlet var webView: UIWebView!

override func viewDidLoad() {
super.viewDidLoad()

let url = Bundle.main.url(forResource: products, withExtension: html)
let request = NSURLRequest(url: url! as URL)
webView.loadRequest(request as URLRequest)
}

@IBAction func closeDocumentViewer() {
displayView.isHidden = true;
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}


If I just one to receive a string from a javascript function what would I have to add to the above?


More From » swift

 Answers
5

You must a custom URL Scheme such as myawesomeapp and intercept requests to it using:



func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool


Fire a call to native code using window.location=myawesomeapp://hello=world, and get the query params you pass from request.URL.query in the native code.



For more information, see my question about UIWebViews here: JavaScript synchronous native communication to WKWebView


[#59946] Monday, November 21, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kristinsonjab

Total Points: 364
Total Questions: 98
Total Answers: 98

Location: Christmas Island
Member since Mon, Oct 19, 2020
4 Years ago
kristinsonjab questions
Fri, Mar 4, 22, 00:00, 2 Years ago
Fri, Jan 22, 21, 00:00, 3 Years ago
Fri, Aug 14, 20, 00:00, 4 Years ago
;