This plugin allows Flutter desktop apps to register and handle custom protocols

Overview

protocol_handler

pub version

This plugin allows Flutter desktop apps to register and handle custom protocols (i.e. deep linking).


English | 简体中文


Platform Support

Linux macOS Windows
✔️ ✔️

Quick Start

Installation

Add this to your package's pubspec.yaml file:

dependencies:
  protocol_handler: ^0.1.1

Or

dependencies:
  protocol_handler:
    git:
      url: https://github.com/leanflutter/protocol_handler.git
      ref: main

Usage

macOS

Change the file macos/Runner/Info.plist as follows:

CFBundleDevelopmentRegion $(DEVELOPMENT_LANGUAGE) CFBundleExecutable $(EXECUTABLE_NAME) CFBundleIconFile CFBundleIdentifier $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion 6.0 CFBundleName $(PRODUCT_NAME) CFBundlePackageType APPL CFBundleShortVersionString $(FLUTTER_BUILD_NAME) CFBundleVersion $(FLUTTER_BUILD_NUMBER) LSMinimumSystemVersion $(MACOSX_DEPLOYMENT_TARGET) NSHumanReadableCopyright $(PRODUCT_COPYRIGHT) NSMainNibFile MainMenu + CFBundleURLTypes + + + CFBundleTypeRole + Editor + CFBundleURLName + + CFBundleURLSchemes + + myprotocol + + + NSPrincipalClass NSApplication }">



	CFBundleDevelopmentRegion
	$(DEVELOPMENT_LANGUAGE)
	CFBundleExecutable
	$(EXECUTABLE_NAME)
	CFBundleIconFile
	
	CFBundleIdentifier
	$(PRODUCT_BUNDLE_IDENTIFIER)
	CFBundleInfoDictionaryVersion
	6.0
	CFBundleName
	$(PRODUCT_NAME)
	CFBundlePackageType
	APPL
	CFBundleShortVersionString
	$(FLUTTER_BUILD_NAME)
	CFBundleVersion
	$(FLUTTER_BUILD_NUMBER)
	LSMinimumSystemVersion
	$(MACOSX_DEPLOYMENT_TARGET)
	NSHumanReadableCopyright
	$(PRODUCT_COPYRIGHT)
	NSMainNibFile
	MainMenu
+	CFBundleURLTypes
+	
+		
+			CFBundleTypeRole
+			Editor
+			CFBundleURLName
+			
+			CFBundleURLSchemes
+			
+				myprotocol
+			
+		
+	
	NSPrincipalClass
	NSApplication


}
Windows

Change the file windows/runner/main.cpp as follows:

int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev, _In_ wchar_t *command_line, _In_ int show_command) { + HWND hwnd = ::FindWindow(L"FLUTTER_RUNNER_WIN32_WINDOW", L"protocol_handler_example"); + if (hwnd != NULL) { + DispatchToProtocolHandler(hwnd); + + ::ShowWindow(hwnd, SW_NORMAL); + ::SetForegroundWindow(hwnd); + return EXIT_FAILURE; + } // Attach to console when present (e.g., 'flutter run') or create a // new console when running with a debugger. if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()) { CreateAndAttachConsole(); } // Initialize COM, so that it is available for use in the library and/or // plugins. ::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); flutter::DartProject project(L"data"); std::vector command_line_arguments = GetCommandLineArguments(); project.set_dart_entrypoint_arguments(std::move(command_line_arguments)); FlutterWindow window(project); Win32Window::Point origin(10, 10); Win32Window::Size size(1280, 720); if (!window.CreateAndShow(L"protocol_handler_example", origin, size)) { return EXIT_FAILURE; } window.SetQuitOnClose(true); ::MSG msg; while (::GetMessage(&msg, nullptr, 0, 0)) { ::TranslateMessage(&msg); ::DispatchMessage(&msg); } ::CoUninitialize(); return EXIT_SUCCESS; }">
#include 
#include 
#include 

#include "flutter_window.h"
#include "utils.h"

+#include 

int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
                      _In_ wchar_t *command_line, _In_ int show_command) {
+  HWND hwnd = ::FindWindow(L"FLUTTER_RUNNER_WIN32_WINDOW", L"protocol_handler_example");
+  if (hwnd != NULL) {
+    DispatchToProtocolHandler(hwnd);
+
+    ::ShowWindow(hwnd, SW_NORMAL);
+    ::SetForegroundWindow(hwnd);
+    return EXIT_FAILURE;
+  }

  // Attach to console when present (e.g., 'flutter run') or create a
  // new console when running with a debugger.
  if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()) {
    CreateAndAttachConsole();
  }

  // Initialize COM, so that it is available for use in the library and/or
  // plugins.
  ::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);

  flutter::DartProject project(L"data");

  std::vector command_line_arguments =
      GetCommandLineArguments();

  project.set_dart_entrypoint_arguments(std::move(command_line_arguments));

  FlutterWindow window(project);
  Win32Window::Point origin(10, 10);
  Win32Window::Size size(1280, 720);
  if (!window.CreateAndShow(L"protocol_handler_example", origin, size)) {
    return EXIT_FAILURE;
  }
  window.SetQuitOnClose(true);

  ::MSG msg;
  while (::GetMessage(&msg, nullptr, 0, 0)) {
    ::TranslateMessage(&msg);
    ::DispatchMessage(&msg);
  }

  ::CoUninitialize();
  return EXIT_SUCCESS;
}
import 'package:protocol_handler/protocol_handler.dart';

void main() async {
  // Must add this line.
  WidgetsFlutterBinding.ensureInitialized();

  // Register a custom protocol
  // For macOS platform needs to declare the scheme in ios/Runner/Info.plist
  await protocolHandler.register('myprotocol');

  runApp(MyApp());
}

Listening events

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with ProtocolListener {
  @override
  void initState() {
    protocolHandler.addListener(this);
    super.initState();
  }

  @override
  void dispose() {
    protocolHandler.removeListener(this);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // ...
  }

  @override
  void onProtocolUrlReceived(String url) {
    String log = 'Url received: $url)';
    print(log);
  }
}

Please see the example app of this plugin for a full example.

Who's using it?

  • Biyi - A convenient translation and dictionary app.

License

MIT

You might also like...

An krunner plugin for changing kde plasma accent color from directly desktop

An krunner plugin for changing kde plasma accent color from directly desktop

Feb 21, 2022

A Flutter package that makes it easy to customize and work with your Flutter desktop app window.

A Flutter package that makes it easy to customize and work with your Flutter desktop app window.

bitsdojo_window A Flutter package that makes it easy to customize and work with your Flutter desktop app window on Windows, macOS and Linux. Watch the

Jan 4, 2023

A Flutter package that makes it easy to customize and work with your Flutter desktop app's system tray.

A Flutter package that makes it easy to customize and work with your Flutter desktop app's system tray.

system_tray A Flutter package that that enables support for system tray menu for desktop flutter apps. on Windows, macOS and Linux. Features: - Modify

Dec 30, 2022

A platform adaptive Flutter app for desktop, mobile and web.

A platform adaptive Flutter app for desktop, mobile and web.

Flutter Folio A demo app showcasing how Flutter can deliver a great multi-platform experience, targeting iOS, Android, MacOS, Windows, Linux, and web.

Jan 2, 2023

TinyPNG4Flutter - A TinyPNG Compress Image Desktop GUI For Flutter. Support macOS and windows

TinyPNG4Flutter - A TinyPNG Compress Image Desktop GUI For Flutter. Support macOS and windows

TinyPNG4Flutter A TinyPNG Compress Image Desktop GUI For Flutter. Support macOS

Dec 8, 2022

Ubuntu Yaru Style - Distinct look and feel of the Ubuntu Desktop

Ubuntu Yaru Style - Distinct look and feel of the Ubuntu Desktop

Ubuntu Yaru Style - Distinct look and feel of the Ubuntu Desktop Using Yaru To be able to use this package follow this steps: Installation Make you su

Dec 28, 2022

An open source desktop application for creating set-plans for TV and movie productions

lyghts_desktop An open source (Windows) desktop application for creating set-plans for TV and movie productions. Getting Started This application uses

Feb 15, 2022

A web dashboard that allows you to monitor your Chia farm and sends notifications when blocks are found and new plots are completed through a discord bot. It can link multiple farmers/harvesters to your account.

A web dashboard that allows you to monitor your Chia farm and sends notifications when blocks are found and new plots are completed through a discord bot. It can link multiple farmers/harvesters to your account.

farmr A web dashboard that allows you to monitor your Chia farm and sends notifications when blocks are found and new plots are completed through a di

Nov 10, 2022

Pangolin Desktop UI shell, designed for dahliaOS, written in Flutter.

Pangolin Desktop UI shell, designed for dahliaOS, written in Flutter.

Website ● Discord ● Releases ● Donate ● Documentation Pangolin Desktop UI Pangolin UI is a Desktop shell for dahliaOS, written in Flutter, runs on Lin

Jan 3, 2023
Comments
  • Expose ProtocolHandlerPlugin so it's possible to call 'handleEventURL' for multiple plugins

    Expose ProtocolHandlerPlugin so it's possible to call 'handleEventURL' for multiple plugins

    We've run into the issue that we have multiple plugins that depend on setting an event handler through NSAppleEventManager.shared().setEventHandler, the next one always overwriting the previous one.

    We are looking into exposing the handler methods on those plugins so we can achieve something like this:

        public func handleURLEvent(_ event: NSAppleEventDescriptor, with replyEvent: NSAppleEventDescriptor) {
            HandlerA.instance.handleURLEvent(...)
            HandlerB.instance.handleURLEvent(...)
           // other handlers
        }
    

    Please let me know what you think of this approach (or if you have any better ideas for our issue)

    opened by JordyLangen 1
  • iOS上处理Url Scheme有问题

    iOS上处理Url Scheme有问题

    这里不能直接返回true,会把其它库的scheme给拦截了。例如:微博登录SDK就无法收到回调了。 https://github.com/leanflutter/protocol_handler/blob/d7e402dbc5af4b269ad94f1abb582ab5c4264041/ios/Classes/SwiftProtocolHandlerPlugin.swift#L35-L40

    opened by feimenggo 0
Releases(v0.1.4)
Owner
LeanFlutter
To make the flutter even simpler
LeanFlutter
This plugin allows Flutter desktop apps to defines system/inapp wide hotkey (i.e. shortcut).

hotkey_manager This plugin allows Flutter desktop apps to defines system/inapp wide hotkey (i.e. shortcut). hotkey_manager Platform Support Quick Star

LeanFlutter 81 Dec 21, 2022
This plugin allows Flutter desktop apps to Retrieve information about screen size, displays, cursor position, etc.

screen_retriever This plugin allows Flutter desktop apps to Retrieve information about screen size, displays, cursor position, etc. screen_retriever P

LeanFlutter 27 Dec 6, 2022
This plugin allows Flutter desktop apps to extract text from screen.

screen_text_extractor This plugin allows Flutter desktop apps to extract text from screen. screen_text_extractor Platform Support Quick Start Installa

LeanFlutter 30 Dec 21, 2022
This plugin allows Flutter desktop apps to defines system tray.

tray_manager This plugin allows Flutter desktop apps to defines system tray. tray_manager Platform Support Quick Start Installation ⚠️ Linux requireme

LeanFlutter 122 Dec 22, 2022
A macOS plugin which can register a callback for a global keyboard shortcut.

global_shortcuts A macOS plugin which can register a callback for a global keyboard shortcut. As the shortcut is global, the callback will be triggere

James Leahy 7 Jan 2, 2023
A Dart/Flutter package to register/query/remove URI Schemes without hassle.

protocol_registry Register/query/remove URI Schemes without hassle. Available for Windows and Linux. Installation flutter pub add protocol_registry Us

ZYROUGE 10 Oct 25, 2022
Build beautiful desktop apps with flutter and rust. 🌠 (wip)

flutter-rs Build flutter desktop app in dart & rust. Get Started Install requirements Rust flutter sdk Develop install the cargo flutter command cargo

null 2k Dec 26, 2022
Build beautiful desktop apps with flutter and rust. 🌠

flutter-rs Build flutter desktop app in dart & rust. Get Started Install requirements Rust flutter sdk Develop install the cargo flutter command cargo

null 2k Dec 26, 2022
Flutter plugin for Flutter desktop(macOS/Linux/Windows) to change window size.

desktop_window Flutter plugin for Flutter desktop(macOS/Linux/Windows) to change window size. Usage import 'package:desktop_window/desktop_window.dart

ChunKoo Park 72 Dec 2, 2022
File picker plugin for Flutter, compatible with mobile (iOS & Android), Web, Desktop (Mac, Linux, Windows) platforms with Flutter Go support.

A package that allows you to use the native file explorer to pick single or multiple files, with extensions filtering support.

Miguel Ruivo 987 Jan 6, 2023