This plugin allows Flutter desktop apps to Auto launch on startup / login.
This fork supports using App Sandbox with macOS.
English | 简体中文
Linux | macOS* | Windows |
---|---|---|
✔️ | ✔️ | ✔️ |
*Required macOS support installation instructions below
Add this to your package's pubspec.yaml file:
dependencies:
launch_at_startup: ^0.2.2
Or
dependencies:
launch_at_startup:
git:
url: https://github.com/leanflutter/launch_at_startup.git
ref: main
import 'dart:io';
import 'package:launch_at_startup/launch_at_startup.dart';
import 'package:package_info_plus/package_info_plus.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
PackageInfo packageInfo = await PackageInfo.fromPlatform();
launchAtStartup.setup(
appName: packageInfo.appName,
appPath: Platform.resolvedExecutable,
);
await launchAtStartup.enable();
await launchAtStartup.disable();
bool isEnabled = await launchAtStartup.isEnabled();
runApp(const MyApp());
}
// ...
Please see the example app of this plugin for a full example.
Add platform channel code to your macos/Runner/MainFlutterWindow.swift
file.
import Cocoa
import FlutterMacOS
// Add the LaunchAtLogin module
import LaunchAtLogin
//
class MainFlutterWindow: NSWindow {
override func awakeFromNib() {
let flutterViewController = FlutterViewController.init()
let windowFrame = self.frame
self.contentViewController = flutterViewController
self.setFrame(windowFrame, display: true)
// Add FlutterMethodChannel platform code
FlutterMethodChannel(
name: "launch_at_startup", binaryMessenger: flutterViewController.engine.binaryMessenger
)
.setMethodCallHandler { (_ call: FlutterMethodCall, result: @escaping FlutterResult) in
switch call.method {
case "launchAtStartupIsEnabled":
result(LaunchAtLogin.isEnabled)
case "launchAtStartupSetEnabled":
if let arguments = call.arguments as? [String: Any] {
LaunchAtLogin.isEnabled = arguments["setEnabledValue"] as! Bool
}
result(nil)
default:
result(FlutterMethodNotImplemented)
}
}
//
RegisterGeneratedPlugins(registry: flutterViewController)
super.awakeFromNib()
}
}
then open your macos/
folder in Xcode and do the following:
Instructions referenced from "LaunchAtLogin" package repository. Read for more details and FAQ's.
macOS 10.13+
Add https://github.com/sindresorhus/LaunchAtLogin
in the “Swift Package Manager” tab in Xcode.
Skip this step if your app targets macOS 13 or later.
Add a new “Run Script Phase” below (not into) “Copy Bundle Resources” in “Build Phases” with the following:
"${BUILT_PRODUCTS_DIR}/LaunchAtLogin_LaunchAtLogin.bundle/Contents/Resources/copy-helper-swiftpm.sh"
And uncheck “Based on dependency analysis”.
The build phase cannot run with "User Script Sandboxing" enabled. With Xcode 15 or newer where it is enabled by default, disable "User Script Sandboxing" in build settings.
(It needs some extra works to have our script to comply with the build phase sandbox.)
(I would name the run script Copy “Launch at Login Helper”
)
- Biyi (比译) - A convenient translation and dictionary app.