Swift Webview

用于 Swift 的跨平台 webview 绑定。

依赖项

根据目标平台,您需要安装一些东西。

macOS

开箱即用™

Linux

您需要安装 libgtk-3-devlibwebkit2gtk-4.0-dev 或您的发行版中的等效项。

sudo apt install libgtk-3-dev libwebkit2gtk-4.0-dev

Windows

Windows 目前未经测试且未正式支持。 欢迎在此处做出贡献。

用法

请参阅生成的文档 此处

基本用法

import SwiftWebview

// create a new webview
let wv = WebView()
      // navigate to a URL
      .navigate("https://example.com")
      // directly set the HTML
      .setHtml("<h1>Hello World</h1>")
      // set the title of the window
      .setTitle("My Webview Window")
      // set the size of the window
      .setSize(800, 600, .None)
      // inject some javascript into every new page
      .inject("console.log('this happens before window.onload')")
      // asynchronously evaluate some JS in the current page
      .eval("console.log('this was evaled at runtime')")

// run the webview
wv.run()

// destroy the webview once we're done with it
wv.destroy()

绑定函数

let wv = WebView()

let mySwiftFunction: JSCallback = { args in
  return "Hello \(args[0])"
}

wv.bind("boundFunction", mySwiftFunction)
wv.run()
var result = window.boundFunction("World");
console.log(result); // Hello World