Flutter应用内链接处理插件uni_links2的使用

发布于 1周前 作者 itying888 来自 Flutter

Flutter应用内链接处理插件uni_links2的使用

安装

要使用该插件,将uni_links添加到你的pubspec.yaml文件作为依赖项。

dependencies:
  uni_links: ^x.x.x

权限

Android 和 iOS 需要在配置文件中声明链接权限。

对于 Android

uni_links支持两种类型的Android链接:“App Links"和"Deep Links”。

  • App Links 只能使用https方案,并且需要指定主机和托管文件assetlinks.json
  • Deep Links 可以使用任何自定义方案,不需要主机或托管文件。缺点是任何应用程序都可以声明一个方案+主机组合,因此确保你的尽可能独特。

你需要在android/app/src/main/AndroidManifest.xml中至少声明以下两个意图过滤器之一:

<intent-filter>
  <action android:name="android.intent.action.VIEW" />
  <category android:name="android.intent.category.DEFAULT" />
  <category android:name="android.intent.category.BROWSABLE" />
  <!-- 接受以 YOUR_SCHEME://YOUR_HOST 开头的URIs -->
  <data android:scheme="[YOUR_SCHEME]" android:host="[YOUR_HOST]" />
</intent-filter>

<intent-filter android:autoVerify="true">
  <action android:name="android.intent.action.VIEW" />
  <category android:name="android.intent.category.DEFAULT" />
  <category android:name="android.intent.category.BROWSABLE" />
  <!-- 接受以 https://YOUR_HOST 开头的URIs -->
  <data android:scheme="https" android:host="[YOUR_HOST]" />
</intent-filter>

对于Deep Links,android:host属性是可选的。

为了进一步提高特定性,可以添加android:pathPrefix属性:

<!-- 接受以 YOUR_SCHEME://YOUR_HOST/NAME/NAME... 开头的URIs -->
<!-- 接受以       https://YOUR_HOST/NAME/NAME... 开头的URIs -->
<!-- 注意前导斜杠是必需的 -->
<data android:scheme="[YOUR_SCHEME_OR_HTTPS]" android:host="[YOUR_HOST]" android:pathPrefix="/[NAME][/NAME...]" />

更多详情可以参考《终极指南》。特别注意有关/.well-known/assetlinks.json文件的部分。

Android开发者文档也是获取更多信息的好来源。

对于 iOS

iOS有两种链接类型:“Universal Links"和"Custom URL schemes”。

  • Universal Links 只能使用https方案,并且需要指定主机、授权和托管文件apple-app-site-association
  • Custom URL schemes 可以使用任何自定义方案,没有主机限制,也没有授权或托管文件。缺点是任何应用程序都可以声明任何方案,因此确保你的尽可能独特。

你需要至少声明其中一种。

Universal Links

你需要通过Xcode或编辑(或创建并添加到Xcode)ios/Runner/Runner.entitlements文件来添加或创建com.apple.developer.associated-domains授权。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <!-- 其他键 -->
  <key>com.apple.developer.associated-domains</key>
  <array>
    <string>applinks:[YOUR_HOST]</string>
  </array>
  <!-- 其他键 -->
</dict>
</plist>

这允许你的应用从https://YOUR_HOST链接启动。

更多信息,可以阅读Apple的《Universal Links》指南。

Custom URL schemes

你需要在ios/Runner/Info.plist中声明方案(或通过Xcode的目标信息编辑器,在URL Types下):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <!-- 其他键 -->
  <key>CFBundleURLTypes</key>
  <array>
    <dict>
      <key>CFBundleTypeRole</key>
      <string>Editor</string>
      <key>CFBundleURLName</key>
      <string>[ANY_URL_NAME]</string>
      <key>CFBundleURLSchemes</key>
      <array>
        <string>[YOUR_SCHEME]</string>
      </array>
    </dict>
  </array>
  <!-- 其他键 -->
</dict>
</plist>

这允许你的应用从YOUR_SCHEME://ANYTHING链接启动。

更多相关信息,可以阅读Apple的《Inter-App Communication》指南。

强烈建议观看Apple WWDC 2015,会议509 - 无缝链接到您的应用,以了解Universal Links的工作原理(以及如何设置)。

使用

你的应用可以通过冷启动和从后台唤醒两种方式接收链接。更多详情参见https://github.com/ericmartineau/uni_links/blob/master/uni_links/example/lib/main.dart

初始链接(字符串)

返回应用启动时的链接(如果有)。

import 'dart:async';
import 'dart:io';

import 'package:uni_links/uni_links.dart';
import 'package:flutter/services.dart' show PlatformException;

Future<void> initUniLinks() async {
  try {
    final initialLink = await getInitialLink();
    // 解析链接并警告用户,如果链接不正确,但请注意它可能是 `null`
  } on PlatformException {
    // 处理异常,警告用户其操作未成功
  }
}

初始链接(Uri)

getInitialLink相同,但转换为Uri

try {
  final initialUri = await getInitialUri();
  // 使用Uri并警告用户,如果链接不正确,但请注意它可能是 `null`
} on FormatException {
  // 处理异常,警告用户其操作未成功
}
// ... 其他异常处理如 PlatformException

也可以通过Uri.parse(initialLink)实现相同功能,这是此便捷方法所做的。

更改事件(字符串)

通常你会检查getInitialLink并监听更改。

import 'dart:async';
import 'dart:io';

import 'package:uni_links/uni_links.dart';

StreamSubscription? _sub;

Future<void> initUniLinks() async {
  // ... 检查 initialLink

  // 添加监听器到流
  _sub = linkStream.listen((String? link) {
    // 解析链接并警告用户,如果链接不正确
  }, onError: (err) {
    // 处理异常,警告用户其操作未成功
  });

  // 注意:不要忘记在 dispose 中调用 _sub.cancel()
}

更改事件(Uri)

linkStream相同,但转换为发射Uri对象。

通常你会检查getInitialUri并监听更改。

import 'dart:async';
import 'dart:io';

import 'package:uni_links/uni_links.dart';

StreamSubscription? _sub;

Future<void> initUniLinks() async {
  // ... 检查 initialUri

  // 添加监听器到流
  _sub = getUriLinksStream().listen((Uri? uri) {
    // 使用Uri并警告用户,如果链接不正确
  }, onError: (err) {
    // 处理异常,警告用户其操作未成功
  });

  // 注意:不要忘记在 dispose 中调用 _sub.cancel()
}

由于这两种情况 - 你应该始终添加对初始链接(或URI)的检查,并订阅链接(或URI)的流。

应用启动时的更多细节

如果应用终止(或未在后台运行),并且操作系统必须重新启动它 - 这是一个冷启动。在这种情况下,getInitialLink将具有启动你的应用的链接,而流在那时不会产生链接。

或者 - 如果应用正在后台运行,并且操作系统必须将其带到前台,那么流将是生成链接的,而getInitialLink将要么是null,要么是启动应用的初始链接。

由于这两个情况,你应该始终添加对初始链接(或URI)的检查,并订阅链接(或URI)的流。

触发链接的工具

如果你注册了一个方案,比如unilink,你可以使用这些命令行工具。

Android

你可以在Android Studio中执行以下任务。

假设你已经安装了Android Studio(带SDK平台工具):

adb shell 'am start -W -a android.intent.action.VIEW -c android.intent.category.BROWSABLE -d "unilinks://host/path/subpath"'
adb shell 'am start -W -a android.intent.action.VIEW -c android.intent.category.BROWSABLE -d "unilinks://example.com/path/portion/?uid=123&token=abc"'
adb shell 'am start -W -a android.intent.action.VIEW -c android.intent.category.BROWSABLE -d "unilinks://example.com/?arr%5b%5d=123&arr%5b%5d=abc&addr=1%20Nowhere%20Rd&addr=Rand%20City%F0%9F%98%82"'

如果你没有adb在路径中,但有$ANDROID_HOME环境变量,则使用"$ANDROID_HOME"/platform-tools/adb ...

注意:或者你可以简单地进入adb shell并在其中运行am命令。

注意:我使用单引号,因为接下来的shell命令将在模拟器(或设备)中运行,并且shell元字符,如问号(?)和与号(&),通常对你自己的shell有不同的含义。

adb shell与唯一可用的设备(或模拟器)通信,所以如果你有多台设备,你必须指定你想运行shell的设备。

  • 唯一USB连接的设备 - adb -d shell '...'
  • 唯一的模拟设备 - adb -e shell '...'

你可以使用adb devices列出当前可用的设备(类似flutter devices做同样的工作)。

iOS

假设你已经安装了Xcode:

/usr/bin/xcrun simctl openurl booted "unilinks://host/path/subpath"
/usr/bin/xcrun simctl openurl booted "unilinks://example.com/path/portion/?uid=123&token=abc"
/usr/bin/xcrun simctl openurl booted "unilinks://example.com/?arr%5b%5d=123&arr%5b%5d=abc&addr=1%20Nowhere%20Rd&addr=Rand%20City%F0%9F%98%82"

更多关于Flutter应用内链接处理插件uni_links2的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter应用内链接处理插件uni_links2的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter应用中使用uni_links2插件来处理应用内链接的示例代码。uni_links2插件允许你监听和处理设备上的应用内链接(URI)。

首先,确保你的Flutter项目已经添加了uni_links2依赖。打开你的pubspec.yaml文件并添加以下依赖:

dependencies:
  flutter:
    sdk: flutter
  uni_links2: ^0.5.0  # 请检查最新版本号

然后运行flutter pub get来安装依赖。

接下来,你需要在你的应用中配置AndroidManifest.xmlInfo.plist来确保应用可以接收URI。

Android 配置

android/app/src/main/AndroidManifest.xml中添加以下intent-filter到你的<activity>标签中:

<activity
    android:name=".MainActivity"
    android:launchMode="singleTop"
    android:theme="@style/LaunchTheme"
    android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|screenLayout|density|uiMode"
    android:hardwareAccelerated="true"
    android:windowSoftInputMode="adjustResize">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="yourappscheme" android:host="yourhost" />
    </intent-filter>
</activity>

yourappschemeyourhost替换为你的实际URI scheme和host。

iOS 配置

ios/Runner/Info.plist中添加以下配置:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>yourappscheme</string>
        </array>
    </dict>
</array>

同样,将yourappscheme替换为你的实际URI scheme。

Flutter 代码实现

接下来,在你的Flutter应用中处理URI。以下是一个简单的示例:

import 'package:flutter/material.dart';
import 'package:uni_links2/uni_links2.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String? _latestLink;

  @override
  void initState() {
    super.initState();
    _initUniLinks();
  }

  Future<void> _initUniLinks() async {
    // Retrieve the latest link
    final link = await getInitialLink();
    if (link != null) {
      setState(() {
        _latestLink = link;
      });
    }

    // Listen for incoming links
    subscribeToLinks(
      onLink: (Uri? uri) async {
        if (uri != null) {
          setState(() {
            _latestLink = uri.toString();
          });
        }
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Uni Links Example'),
        ),
        body: Center(
          child: Text(
            _latestLink ?? 'No link received yet.',
            style: TextStyle(fontSize: 20),
          ),
        ),
      ),
    );
  }
}

在这个示例中,我们首先尝试获取启动应用时的初始链接(如果有的话),然后订阅后续的链接事件。每当应用接收到一个新的链接时,我们更新UI以显示该链接。

运行应用

确保你的设备或模拟器能够接收URI链接。在Android上,你可以通过ADB命令发送一个URI:

adb shell am start -a android.intent.action.VIEW -d "yourappscheme://yourhost/somepath"

在iOS上,你可以通过Safari或其他浏览器打开该URI。

这就是如何使用uni_links2插件在Flutter应用中处理应用内链接的基本步骤。希望这对你有所帮助!

回到顶部