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:
protocol_handler: ^0.2.0
Or
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:
android:icon="@mipmap/ic_launcher"
android:label="protocol_handler_example">
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:resource="@style/NormalTheme" />
+
+
+
+
+
+
+
+
android:value="2" />
iOS
Change the file ios/Runner/Info.plist as follows:
+
+
+
+
+
+
+
+
+
+
+
+
+
macOS
Change the file macos/Runner/Info.plist as follows:
+
+
+
+
+
+
+
+
+
+
+
+
+
Windows
Change the file windows/runner/main.cpp as follows:
#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
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:
protocol_activation: myprotocol
See this issue for details: YehudaKremer/msix#187
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
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.