Swift webview:如何从javascript中正确调用swift代码?

编程入门 行业动态 更新时间:2024-10-23 00:25:02
本文介绍了Swift webview:如何从javascript中正确调用swift代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试让我的javascript与swift代码进行交互但不幸的是我没有成功。

I'm trying to make my javascript interact with swift code but unfortunately i didn't succeed.

目前,我只是尝试更改标题颜色并显示一条消息,就像您将在下面的代码中看到的那样。

For the moment, i just tried to change the headers colors and display a message like you will see in the code below.

这是我的( index.html )代码:

<!DOCTYPE html> <html> <head> <title>Test</title> <meta charset="UTF-8"> </head> <body> <h1>WebView Test</h1> <script type="text/javascript" src="main.js"></script> </body> </html>

这是我的( main.js -JavaScript )代码:

function callNativeApp () { try { webkit.messageHandlers.callbackHandler.postMessage("Send from JavaScript"); } catch(err) { console.log('error'); } } setTimeout(function () { callNativeApp(); }, 5000); function redHeader() { document.querySelector('h1').style.color = "red"; }

这是我的( ViewController.swift )代码:

import UIKit import WebKit class ViewController: UIViewController, WKScriptMessageHandler { @IBOutlet var containerView : UIView! = nil var webView: WKWebView? override func loadView() { super.loadView() var contentController = WKUserContentController(); var userScript = WKUserScript( source: "redHeader()", injectionTime: WKUserScriptInjectionTime.AtDocumentEnd, forMainFrameOnly: true ) contentController.addUserScript(userScript) contentController.addScriptMessageHandler( self, name: "callbackHandler" ) var config = WKWebViewConfiguration() config.userContentController = contentController self.webView = WKWebView() self.view = self.webView! } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. var url = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("index", ofType: "html")!) var req = NSURLRequest(URL: url) self.webView!.loadRequest(req) } func userContentController(userContentController: WKUserContentController!,didReceiveScriptMessage message: WKScriptMessage!) { if(message.name == "callbackHandler") { println("JavaScript is sending a message \(message.body)") } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }

推荐答案

您已正确设置所有内容,但未将 WKWebViewConfiguration 实例提供给 WKWebView 。由于配置具有Javascript / Swift桥的详细信息,因此无法来回通话。

You have everything set up properly, but you aren't giving your WKWebViewConfiguration instance to the WKWebView. Since the configuration has the details of the Javascript/Swift bridge, you can't talk back and forth.

override func loadView() { // ... var config = WKWebViewConfiguration() config.userContentController = contentController self.webView = WKWebView(frame: self.view.frame, configuration: config) self.view = self.webView! }

更多推荐

Swift webview:如何从javascript中正确调用swift代码?

本文发布于:2023-11-26 20:15:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635033.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确   代码   webview   Swift   swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!