Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/leanflutter/protocol_handler

This plugin allows Flutter apps to register and handle custom protocols (i.e. deep linking).
https://github.com/leanflutter/protocol_handler

deep-linking deeplinking

Last synced: 4 days ago
JSON representation

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

Awesome Lists containing this project

README

        

# protocol_handler

[![pub version][pub-image]][pub-url] [![][discord-image]][discord-url] ![][visits-count-image]

[pub-image]: https://img.shields.io/pub/v/protocol_handler.svg
[pub-url]: https://pub.dev/packages/protocol_handler
[discord-image]: https://img.shields.io/discord/884679008049037342.svg
[discord-url]: https://discord.gg/zPa6EZ2jqb
[visits-count-image]: https://img.shields.io/badge/dynamic/json?label=Visits%20Count&query=value&url=https://api.countapi.xyz/hit/leanflutter.protocol_handler/visits

这个插件允许 Flutter 应用注册及处理自定义协议(即深度链接)。

---

[English](./README.md) | 简体中文

---

- [平台支持](#%E5%B9%B3%E5%8F%B0%E6%94%AF%E6%8C%81)
- [截图](#%E6%88%AA%E5%9B%BE)
- [快速开始](#%E5%BF%AB%E9%80%9F%E5%BC%80%E5%A7%8B)
- [安装](#%E5%AE%89%E8%A3%85)
- [用法](#%E7%94%A8%E6%B3%95)
- [Android](#android)
- [iOS](#ios)
- [macOS](#macos)
- [Windows](#windows)
- [监听事件](#%E7%9B%91%E5%90%AC%E4%BA%8B%E4%BB%B6)
- [谁在用使用它?](#%E8%B0%81%E5%9C%A8%E7%94%A8%E4%BD%BF%E7%94%A8%E5%AE%83)
- [许可证](#%E8%AE%B8%E5%8F%AF%E8%AF%81)

## 平台支持

| Android | iOS | Linux | macOS | Windows |
| :-----: | :-: | :---: | :---: | :-----: |
| ✔️ | ✔️ | ➖ | ✔️ | ✔️ |

## 截图

https://user-images.githubusercontent.com/3889523/167283452-aff2535b-c322-45c7-949f-d1c80c2e4a60.mp4

## 快速开始

### 安装

将此添加到你的软件包的 pubspec.yaml 文件:

```yaml
dependencies:
protocol_handler: ^0.2.0
```

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

### 用法

##### Android

更改文件 `android/app/src/main/AndroidManifest.xml` 如下:

```diff








+
+
+
+
+
+
+
+



```

##### iOS

更改文件 `ios/Runner/Info.plist` 如下:

```diff

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

更改文件 `macos/Runner/Info.plist` 如下:

```diff

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

更改文件 `windows/runner/main.cpp` 如下:

```diff
#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;
}
```

如果你使用 `MSIX` 打包应用,需要在 `msix_config` 中添加 `protocol_activation` 配置:

```yaml
msix_config:
protocol_activation: myprotocol
```

```dart
import 'package:protocol_handler/protocol_handler.dart';

void main() async {
// 必须加上这一行。
WidgetsFlutterBinding.ensureInitialized();

// 注册一个自定义协议。
// 对于 macOS 平台需要在 ios/Runner/Info.plist 中声明 scheme。
await protocolHandler.register('myprotocol');

runApp(MyApp());
}
```

### 监听事件

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

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

class _HomePageState extends State 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);
}
}
```

> 请看这个插件的示例应用,以了解完整的例子。

## 谁在用使用它?

- [比译](https://biyidev.com/) - 一个便捷的翻译和词典应用程序。

## 许可证

[MIT](./LICENSE)