Swiftpack.co - octree/SwiftWKBridge as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by octree.
octree/SwiftWKBridge 1.5.1
An elegant way to send message between Swift and WKWebView
⭐️ 9
🕓 6 weeks ago
iOS macOS
.package(url: "https://github.com/octree/SwiftWKBridge.git", from: "1.5.1")

SwiftWKBridge

Version License Platform SPM

An elegant way to send message between Swift and WKWebView.

You don't need to write any javascript code.

And the web developer don't need write any extra javascript code either.

Example

It's very easy to define a javascript function with native code

let plg: (String) -> Void = {
	print("🌏 [WebView]:", $0)
}
// the first arg is the function name
webView.injector.inject(path: "window.bridge.log", plugin: plg)
// the web developer can invoke this function directly
// window.bridge.log("hello world");

define a function with callbacks

let plg: (String, Callback) -> Void = {
    $1("Got it. ", $0)
}
// Subscripts is supported
webView.injector["window.bridge.test"] = plg
// js: window.bridge.test("message", console.log)

Codable

struct User: Codable {
    var name: String
    var age: Int
    var nickname: String?
}

let plg: (User, Callback) -> Void = { user, callback in
    var user = user
    user.nickname = "Nickname"
    user.age = 10
    callback(user)
}

webView.injector["window.bridge.test"] = plg

// window.bridge.test({ name: "Octree", age: 100 }, (user) => { /* ... */ })

Async

let read: (String) async throws -> String = {
  "World"
}

webView.injector.inject(path: "bridge.read", plugin: read)
// await bridge.read("hello")

CSS Injector

Besides, you can inject css into your webView

webView.injector.cssInjector.inject(css: source, forKey: key)
// remove css
webView.injector.cssInjector.removeCSS(forKey: key)

Night Mode

webView.nightFall()
webView.sunrise()

Installation

CocoaPods

pod 'SwiftWKBridge'

Swift Package Manager

Author

Octree, [email protected]

License

SwiftWKBridge is available under the MIT license. See the LICENSE file for more info.

GitHub

link
Stars: 9
Last commit: 6 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics