在iOS中使用Swift获取Javascript函数响应
我想弄清楚如何在我的iOS应用程序中使用swift平台实现JavaScript函数 。
我已经尝试了链接中指定的示例代码
https://tetontech.wordpress.com/2014/07/15/swift-to-javascript-and-javascript-to-swift-a-round-trip/
但我想要以同样的方式,像android
所做的一样,在http://android-er.blogspot.in/2011/10/call-javascript-inside-webview-from.html
你能帮我一下吗?
任何帮助将深深appriciated。
如果您使用的是WKWebView
您可以通过使用来调用JavaScript
webView.evaluateJavaScript("YourJavaScript",completionHandler : nil )
对于UIWebView
你可以使用
webview.stringByEvaluatingJavascriptFromString : @"YourJavaScript";
这两个环节( 第一 , 第二 )帮助我解决了我的问题。
html / javascript代码:
function bar(qq){ document.getElementById('foo').innerHTML = qq; } <div id="foo"></div>
我的ViewController看起来像这样:
class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate { var webView: WKWebView required init(coder aDecoder: NSCoder) { self.webView = WKWebView(frame: CGRect.zero) super.init(coder: aDecoder)! } override func loadView() { super.loadView() let userContentController = WKUserContentController() let source = "bar('Hello from swift');" let userScript = WKUserScript(source: source, injectionTime: WKUserScriptInjectionTime.atDocumentEnd, forMainFrameOnly: true) userContentController.addUserScript(userScript) let configuration = WKWebViewConfiguration() configuration.userContentController = userContentController self.webView = WKWebView(frame: self.view.frame, configuration: configuration) } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. view.addSubview(webView) webView.translatesAutoresizingMaskIntoConstraints = false let height = NSLayoutConstraint(item: webView, attribute: .height, relatedBy: .equal, toItem: view, attribute: .height, multiplier: 1, constant: 0) let width = NSLayoutConstraint(item: webView, attribute: .width, relatedBy: .equal, toItem: view, attribute: .width, multiplier: 1, constant: 0) view.addConstraints([height, width]) let path = Bundle.main.path(forResource: "foldername/index", ofType: "html")! let url = URL(fileURLWithPath: path) webView.load(URLRequest(url: url)) webView.allowsBackForwardNavigationGestures = true } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
使用webviewjavascriptbridge,以便您可以直接使用那里预定义的方法,并将js方法名称传入该方法。
示例:将js代码下面的粘贴复制到您的js文件中
function setupWebViewJavascriptBridge(callback) { if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); } if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); } window.WVJBCallbacks = [callback]; var WVJBIframe = document.createElement('iframe'); WVJBIframe.style.display = 'none'; WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__'; document.documentElement.appendChild(WVJBIframe); setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0) }
将webview传递给下面的函数:
[WebViewJavascriptBridge bridgeForWebView:webView];
方法 :
[bridge registerHandler:(NSString*)handlerName handler:(WVJBHandler)handler] [bridge callHandler:(NSString*)handlerName data:(id)data responseCallback:(WVJBResponseCallback)callback]
调用bridge.disableJavscriptAlertBoxSafetyTimeout()
与调用[bridge disableJavscriptAlertBoxSafetyTimeout];
具有相同的效果[bridge disableJavscriptAlertBoxSafetyTimeout];
在ObjC。 事情是Js注册处理程序意味着ios的callhandler和ios调用处理程序是js文件注册处理程序。
例如:
bridge.disableJavscriptAlertBoxSafetyTimeout()
点击这里了解更多
var AppiwebScript = function() {//in some_file.js //TO DO }; //below is code for xcode let jScript = "AppiwebScript();"//name js function, in js file let str_val = mainWebView.stringByEvaluatingJavaScript(from: jScript)//handeleng js function
- 无法在applicationWillResignActive中获取当前ViewController
- 我无法select要在iTunes Connect上的TestFlight中testing的iOS版本
- 使用swift在UIView上添加阴影3
- 致命错误:在展开可选值(lldb)时意外发现nil
- 在swift中检查数组元素
- 我怎样才能找出Objective-C的genericstypes?
- IntelXDK:在admobpro插件上IOS Build失败
- 具有多个操作的UIButton:如何防止其他操作被触发
- xcodebuild:cdtool无法编译:DataModelCompile /path/to/coredatamodel.xcdatamodeld dyld:找不到符号:_OBJC_CLASS _ $ _ OS_object
- 使用圆角保存UIImage并使用Swift边框
- 用于位置更新多个视图的单身人士