use*_*986 2 javascript uiwebview swift
如果有人问我但我无法找到任何答案或解决方案,我道歉.我期待从"UIWebView"调用JavaScript函数并在swift中监听它.我找到的任何例子都使用"WKWebView".必须有一种简单的方法来收听如下的JavaScript函数:
// HTML / JS
<div id ="myDiv" onclick="listenInSwift()">myItem</div>
Run Code Online (Sandbox Code Playgroud)
这可能是UIWebView吗?谢谢大家!
pau*_*lvs 13
实现listenInSwift如下:
function listenInSwift() {
window.location = 'yoururlscheme://somehost?greeting=hello'
}
Run Code Online (Sandbox Code Playgroud)
然后在您的UIWebViewDelegate班级中使用此代码收听此网址:
func webView(_ webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType navigationType: UIWebViewNavigationType) -> Bool {
if request.URL.scheme == 'yoururlscheme' {
print('Hello JavaScript...')
}
}
Run Code Online (Sandbox Code Playgroud)
不要忘记在Xcode中注册您的URL Scheme(在本例中为'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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11437 次 |
| 最近记录: |