listenInSwift像这样实现:
function listenInSwift() { window.location = 'yoururlscheme://somehost?greeting=hello'}然后在您的
UIWebViewDelegate课程中使用以下代码来监听此URL :
func webView(_ webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType navigationType: UIWebViewNavigationType) -> Bool { if request.URL.scheme == 'yoururlscheme' { print('Hello Javascript...') }}不要忘记在Xpre中注册您的URL方案(在本例中为“ yoururlscheme”)。
要在Web视图中加载本地文件,请尝试以下操作:
let baseURL = NSURL.fileURLWithPath(NSBundle.mainBundle().bundlePath);let relativePath = "www/(file)"let fileURL = NSURL(string: relativePath, relativeToURL: baseURL);let URLRequest = NSURLRequest(URL: fileURL!);webView.loadRequest(URLRequest)



