3 回复
抱歉,作为屌丝程序员,我还没研究那么深。建议百度搜索相关博客,有很多大佬分享过经验。
更多关于Flutter深度链接集成教程的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
在Flutter中集成深度链接(Deep Linking)可以让你通过URL直接打开应用中的特定页面。以下是实现Flutter深度链接的步骤:
1. 添加依赖
首先,在 pubspec.yaml
文件中添加 uni_links
依赖:
dependencies:
flutter:
sdk: flutter
uni_links: ^0.5.1
然后运行 flutter pub get
来安装依赖。
2. 配置Android和iOS
Android
在 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" />
<data android:host="example.com" android:scheme="https" />
<data android:host="example.com" android:scheme="http" />
</intent-filter>
iOS
在 ios/Runner/Info.plist
文件中,添加以下配置:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>com.example.app</string>
<key>CFBundleURLSchemes</key>
<array>
<string>example</string>
</array>
</dict>
</array>
<key>FlutterDeepLinkingEnabled</key>
<true/>
3. 处理深度链接
在Flutter应用中处理深度链接的代码如下:
import 'package:flutter/material.dart';
import 'package:uni_links/uni_links.dart';
import 'package:flutter/services.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _link = 'Unknown';
@override
void initState() {
super.initState();
initUniLinks();
}
Future<void> initUniLinks() async {
try {
String initialLink = await getInitialLink();
if (initialLink != null) {
setState(() {
_link = initialLink;
});
}
getUriLinksStream().listen((Uri uri) {
setState(() {
_link = uri.toString();
});
}, onError: (err) {
print('Error: $err');
});
} on PlatformException {
print('Failed to get initial link.');
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Deep Link Example'),
),
body: Center(
child: Text('Link: $_link'),
),
),
);
}
}
4. 测试深度链接
- Android: 使用
adb
命令测试深度链接:adb shell am start -W -a android.intent.action.VIEW -d "https://example.com" com.example.app
- iOS: 在Safari中输入
example://
来测试深度链接。
通过以上步骤,你可以在Flutter应用中成功集成深度链接功能。