https://github.com/linusu/flutter_jsbridge
Bridge your JavaScript library for usage in Flutter 🚀
https://github.com/linusu/flutter_jsbridge
Last synced: about 1 year ago
JSON representation
Bridge your JavaScript library for usage in Flutter 🚀
- Host: GitHub
- URL: https://github.com/linusu/flutter_jsbridge
- Owner: LinusU
- License: mit
- Created: 2019-03-06T10:44:19.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-04-11T16:19:22.000Z (about 7 years ago)
- Last Synced: 2025-05-07T23:44:53.614Z (about 1 year ago)
- Language: Swift
- Size: 87.9 KB
- Stars: 9
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- Changelog: changelog.md
- License: LICENSE
Awesome Lists containing this project
README
# JSBridge for Flutter
Bridge your JavaScript library for usage in Flutter 🚀
## Usage
```dart
import 'package:flutter_jsbridge/jsbridge.dart';
final _libraryCode = """
window.Foobar = {
add (a, b) {
return a + b
},
greet (name) {
return `Hello, \${name}!`
},
async fetch (url) {
const response = await fetch(url)
const body = await response.text()
return { status: response.status, body }
}
}
""";
class FetchResponse {
final int status;
final String body;
FetchResponse(dynamic data): status = data['status'], body = data['body'];
}
class Foobar {
static final _bridge = JSBridge(_libraryCode);
static Future add(int lhs, int rhs) async {
return await Foobar._bridge.call("Foobar.add", [lhs, rhs]);
}
static Future greet(String name) async {
return await Foobar._bridge.call("Foobar.greet", [name]);
}
static Future fetch(String url) async {
return FetchResponse(await Foobar._bridge.call("Foobar.fetch", [url]));
}
}
```
## iOS
To be able to use JSBridge on iOS, you need to give JSBridge a hook to your view hierarchy. Otherwise the `WKWebView` will get suspended by the OS, and your Promises will never settle.
This is accomplished by using the `setGlobalUIHook` function before instantiating any `JSBridge` instances.
**Flutter:**
If you have a non-modified Flutter application, this should be added to `ios/Runner/AppDelegate.swift`, in the `application(_:didFinishLaunchingWithOptions:)` function.
```diff
--- a/ios/Runner/AppDelegate.swift
+++ b/ios/Runner/AppDelegate.swift
@@ -1,5 +1,6 @@
import UIKit
import Flutter
+import flutter_jsbridge
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
) -> Bool {
+ SwiftFlutterJsbridgePlugin.setGlobalUIHook(window: UIApplication.shared.windows.first!)
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
```
**App:**
```swift
// Can be called from anywhere, e.g. your AppDelegate
SwiftFlutterJsbridgePlugin.setGlobalUIHook(window: UIApplication.shared.windows.first!)
```
**App Extension:**
```swift
// From within your root view controller
SwiftFlutterJsbridgePlugin.setGlobalUIHook(viewController: self)
```