跳转至正文

深度链接

当应用收到新 URL 时导航到对应路由。

深度链接不仅能打开应用,还能将用户带到应用内「深层」的特定位置。例如,一双运动鞋广告中的深度链接可能会打开购物应用,并显示那双鞋的商品页面。

Flutter 在 iOS、Android 和 Web 上均支持深度链接。打开 URL 会在你的应用中显示对应界面。按照以下步骤,你可以使用命名路由(通过 routes 参数或 onGenerateRoute),或使用 Router widget 来启动并显示路由。

若在 Web 浏览器中运行应用,无需额外配置。路由路径的处理方式与 iOS 或 Android 深度链接相同。默认情况下,Web 应用使用 /#/path/to/app/screen 模式从 URL 片段读取深度链接路径,但你可以通过配置应用的 URL 策略来更改这一点。

若你偏好视觉学习,请观看以下视频:

Watch on YouTube in a new tab: "Deep linking in Flutter"

入门

#

入门请参阅我们为 Android 和 iOS 准备的 Cookbook 教程:

从基于 plugin 的深度链接迁移

#

若你已按 Medium 上的免费文章 Deep Links and Flutter applications 所述编写了处理深度链接的 plugin,应退出 Flutter 的默认深度链接处理器。为此,在 Info.plist 中将 FlutterDeepLinkingEnabled 设为 false,AndroidManifest.xml 中将 flutter_deeplinking_enabled 设为 false。

行为

#

行为会因平台以及应用是否已启动并运行而略有不同。

Platform / ScenarioUsing NavigatorUsing Router
iOS (not launched) App gets initialRoute ("/") and a short time after gets a pushRoute App gets initialRoute ("/") and a short time after uses the RouteInformationParser to parse the route and call RouterDelegate.setNewRoutePath, which configures the Navigator with the corresponding Page.
Android - (not launched) App gets initialRoute containing the route ("/deeplink") App gets initialRoute ("/deeplink") and passes it to the RouteInformationParser to parse the route and call RouterDelegate.setNewRoutePath, which configures the Navigator with the corresponding Pages.
iOS (launched) pushRoute is called Path is parsed, and the Navigator is configured with a new set of Pages.
Android (launched) pushRoute is called Path is parsed, and the Navigator is configured with a new set of Pages.
平台 / 场景使用 Navigator使用 Router
iOS(未启动) 应用获得 initialRoute("/"),稍后收到 pushRoute 应用获得 initialRoute("/"),稍后使用 RouteInformationParser 解析路由并调用 RouterDelegate.setNewRoutePath,从而用对应的 Page 配置 Navigator。
Android(未启动) 应用获得包含路由("/deeplink")的 initialRoute 应用获得 initialRoute("/deeplink")并传给 RouteInformationParser 解析路由,调用 RouterDelegate.setNewRoutePath,从而用对应的 Pages 配置 Navigator。
iOS(已启动)调用 pushRoute解析路径,并用新的 Pages 集合配置 Navigator。
Android(已启动)调用 pushRoute解析路径,并用新的 Pages 集合配置 Navigator。

使用 Router widget 时,当应用运行期间打开新的深度链接,你的应用可以替换当前的页面集合。

延伸阅读

#