Dark Mode

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

leanflutter/protocol_handler

Repository files navigation

Ship Your App Faster: Try Fastforge - The simplest way to build, package and distribute your Flutter apps.

protocol_handler

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


English | Jian Ti Zhong Wen


  • Platform Support
  • Screenshots
  • Quick Start
    • Installation
    • Usage
      • Android
      • iOS
      • macOS
      • Windows
    • Listening events
  • Who's using it?
  • License

Platform Support

Android iOS Linux macOS Windows

Screenshots

protocol_handler_windows.mp4

Quick Start

Installation

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

dependencies:
protocol_handler: ^0.2.0

Or

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

Usage

Android

Change the file android/app/src/main/AndroidManifest.xml as follows:

"> package="dev.leanflutter.plugins.protocol_handler_example">

android:name="${applicationName}"
android:icon="@mipmap/ic_launcher"
android:label="protocol_handler_example">
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:exported="true"
android:hardwareAccelerated="true"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:windowSoftInputMode="adjustResize">

android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme" />





+
+
+
+
+
+
+
+


android:name="flutterEmbedding"
android:value="2" />

iOS

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





CFBundleDevelopmentRegion
$(DEVELOPMENT_LANGUAGE)
CFBundleDisplayName
Protocol Handler
CFBundleExecutable
$(EXECUTABLE_NAME)
CFBundleIdentifier
$(PRODUCT_BUNDLE_IDENTIFIER)
CFBundleInfoDictionaryVersion
6.0
CFBundleName
protocol_handler_example
CFBundlePackageType
APPL
CFBundleShortVersionString
$(FLUTTER_BUILD_NAME)
CFBundleSignature
????
CFBundleVersion
$(FLUTTER_BUILD_NUMBER)
LSRequiresIPhoneOS

UILaunchStoryboardName
LaunchScreen
UIMainStoryboardFile
Main
+ CFBundleURLTypes
+
+
+ CFBundleTypeRole
+ Editor
+ CFBundleURLName
+
+ CFBundleURLSchemes
+
+ myprotocol
+
+

+
UISupportedInterfaceOrientations

UIInterfaceOrientationPortrait
UIInterfaceOrientationLandscapeLeft
UIInterfaceOrientationLandscapeRight

UISupportedInterfaceOrientations~ipad

UIInterfaceOrientationPortrait
UIInterfaceOrientationPortraitUpsideDown
UIInterfaceOrientationLandscapeLeft
UIInterfaceOrientationLandscapeRight

UIViewControllerBasedStatusBarAppearance



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

Windows

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

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) {
+ // Replace protocol_handler_example with your_window_title.
+ 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;
}

If you use MSIX to package your application, you need to add protocol_activation configuration in msix_config:

msix_config:
protocol_activation: myprotocol

See this issue for details: YehudaKremer/msix#187

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
State<HomePage> 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

About

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

Topics

Resources

Readme

License

MIT license

Stars

Watchers

Forks

Sponsor this project

Contributors