Skip to content

An open-source cross-platform alternative to AirDrop

fix(macos): prevent method channel race during startup

ShlomoCode5dde378ab606
diff --git a/app/assets/CHANGELOG.md b/app/assets/CHANGELOG.md
index e742e9a5..2e5cb920 100644
--- a/app/assets/CHANGELOG.md
+++ b/app/assets/CHANGELOG.md
@@ -1,5 +1,6 @@
## Unreleased
+- fix(macos): prevent intermittent MissingPluginException during startup (@ShlomoCode)
- fix(macos): file sharing via Share Extension doesn't work in DMG builds (@ShlomoCode, @vdmkotai)
- fix: show the real error when a file cannot be read while sending, instead of transferring it as an empty file (@leopalmieri-spec)
- fix: show the actual save location path for receiving files (@ShlomoCode)
diff --git a/app/macos/Runner/AppDelegate.swift b/app/macos/Runner/AppDelegate.swift
index 49f2d859..38f741b5 100644
--- a/app/macos/Runner/AppDelegate.swift
+++ b/app/macos/Runner/AppDelegate.swift
@@ -17,6 +17,7 @@ class AppDelegate: FlutterAppDelegate {
private var pendingFilesObservation: Defaults.Observation?
private var pendingStringsObservation: Defaults.Observation?
private var isLaunchedAsLoginItem: Bool?
+ private var pendingLoginItemResults: [FlutterResult] = []
override func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
return true
@@ -27,17 +28,22 @@ class AppDelegate: FlutterAppDelegate {
return false
}
- override func applicationDidFinishLaunching(_ notification: Notification) {
- let controller = mainFlutterWindow?.contentViewController as! FlutterViewController
- channel = FlutterMethodChannel(name: "main-delegate-channel", binaryMessenger: controller.engine.binaryMessenger)
+ func registerMethodChannel(binaryMessenger: FlutterBinaryMessenger) {
+ channel = FlutterMethodChannel(name: "main-delegate-channel", binaryMessenger: binaryMessenger)
channel?.setMethodCallHandler(handleFlutterCall)
-
+ }
+
+ override func applicationDidFinishLaunching(_ notification: Notification) {
NSApplication.shared.servicesProvider = self
let localsendBrandColor = NSColor(red: 0, green: 0.392, blue: 0.353, alpha: 0.8) // #00645a
DockProgress.style = .squircle(color: localsendBrandColor)
isLaunchedAsLoginItem = LaunchAtLogin.wasLaunchedAtLogin
+ for result in pendingLoginItemResults {
+ result(isLaunchedAsLoginItem)
+ }
+ pendingLoginItemResults.removeAll()
restoreDestinationFolderAccess()
}
@@ -184,7 +190,12 @@ class AppDelegate: FlutterAppDelegate {
result(FlutterError(code: "INVALID_ARGUMENT", message: "Expected a boolean value", details: nil))
}
case "isLaunchedAsLoginItem":
- result(isLaunchedAsLoginItem)
+ if let isLaunchedAsLoginItem = isLaunchedAsLoginItem {
+ result(isLaunchedAsLoginItem)
+ } else {
+ // The launch Apple event is only available in `applicationDidFinishLaunching`.
+ pendingLoginItemResults.append(result)
+ }
case "isReduceMotionEnabled":
result(NSWorkspace.shared.accessibilityDisplayShouldReduceMotion)
case "openFirewallSettings":
diff --git a/app/macos/Runner/MainFlutterWindow.swift b/app/macos/Runner/MainFlutterWindow.swift
index c9700270..e1ca3ea1 100644
--- a/app/macos/Runner/MainFlutterWindow.swift
+++ b/app/macos/Runner/MainFlutterWindow.swift
@@ -5,6 +5,12 @@ import window_manager
class MainFlutterWindow: NSWindow {
override func awakeFromNib() {
let flutterViewController = FlutterViewController.init()
+
+ // Register before installing the view controller: Dart can start before `applicationDidFinishLaunching`
+ // and call this channel, causing MissingPluginException if registration waits until then.
+ let appDelegate = NSApplication.shared.delegate as! AppDelegate
+ appDelegate.registerMethodChannel(binaryMessenger: flutterViewController.engine.binaryMessenger)
+
let windowFrame = self.frame
self.contentViewController = flutterViewController
self.setFrame(windowFrame, display: true)