参考文章:[Flutter]微信分享并从分享链接跳回APP指定页面 作者:huos08

Flutter 中使用到的插件:

  1. fluwx: 2.0.7 : 包含分享链接、文本、图片等到微信,还有微信支付。
  2. uni_links: 0.4.0 : 通过链接可以激活应用程序,并且可能包含一些信息,可以使用这些信息来加载应用程序的特定部分或继续进行网站。

微信分享

使用 fluwx: 2.0.7 实现微信分享。

依赖配置

  1. dependencies:
  2. # 微信支付
  3. fluwx: 2.0.7

Android 配置

不需要特殊配置

IOS 配置

需要配置 universalLink ,参考 文档 以便了解如何生成通用链接,你也可以学习到怎么在iOS工程中添加URL schema,怎么添加LSApplicationQueriesSchemes。这很重要。

Flutter 使用

注册微信sdk,如果用于ios,必须指定 universalLink

  1. import 'package:fluwx/fluwx.dart' as fluwx;
  2. @override
  3. void initState() {
  4. fluwx.registerWxApi(
  5. appId: your wechat appid,
  6. doOnAndroid: true,
  7. doOnIOS: true,
  8. universalLink: your universal link
  9. );
  10. }

以分享网页链接到好友为例:

  1. fluwx.shareToWeChat(fluwx.WeChatShareWebPageModel(
  2. your web url,
  3. title: your title,
  4. description: your desc,
  5. thumbnail: fluwx.WeChatImage.network(your image url),
  6. ));

至此,已经实现从 APP 分享到微信,下一步将从微信点击分享后的链接,唤起APP并直接打开指定页面。

通过分享链接跳回 APP

使用 uni_links 实现

依赖配置

  1. dependencies:
  2. uni_links: 0.4.0

Android 配置

android/app/src/main/AndroidManifest.xml

  1. <activity
  2. android:name=".MainActivity"
  3. android:launchMode="singleTop"
  4. android:theme="@style/LaunchTheme"
  5. android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
  6. android:hardwareAccelerated="true"
  7. android:windowSoftInputMode="adjustResize">
  8. <meta-data
  9. android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
  10. android:value="true" />
  11. <intent-filter>
  12. <action android:name="android.intent.action.MAIN"/>
  13. <category android:name="android.intent.category.LAUNCHER"/>
  14. </intent-filter>
  15. <!--这里是需要添加的部分-->
  16. <intent-filter>
  17. <action android:name="android.intent.action.VIEW" />
  18. <category android:name="android.intent.category.DEFAULT" />
  19. <category android:name="android.intent.category.BROWSABLE" />
  20. <data
  21. android:scheme="yourScheme"
  22. android:host="yourHost"
  23. />
  24. </intent-filter>
  25. <!--这里是需要添加的部分结束位置-->
  26. </activity>

IOS 配置

参考 uni_links 文档

Flutter 使用

在 APP 登录后的主页面对 URI 进行解析,并跳转到对应页面,如:

  1. class MainPage extends StatefulWidget {
  2. @override
  3. State<StatefulWidget> createState() => MainPageState();
  4. }
  5. class MainPageState extends State<MainPage> {
  6. StreamSubscription _sub;
  7. Uri _latestUri;
  8. // This widget is the root of your application.
  9. @override
  10. void initState() {
  11. super.initState();
  12. initPlatformState();
  13. }
  14. @override
  15. void dispose() {
  16. if (_sub != null) _sub.cancel();
  17. super.dispose();
  18. }
  19. Future<void> initPlatformState() async {
  20. _sub = getLinksStream().listen((String link) {
  21. debugPrint('got link: $link');
  22. try {
  23. if (link != null) _latestUri = Uri.parse(link);
  24. _parseUriAndGetWaresInfoAndTurn();
  25. } on FormatException catch (e) {
  26. debugPrint("[getLinksStream.listen] FormatException: $e");
  27. }
  28. }, onError: (err) {
  29. debugPrint('got err: $err');
  30. _latestUri = null;
  31. });
  32. String initialLink;
  33. Uri initialUri;
  34. try {
  35. initialLink = await getInitialLink();
  36. print('initial link: $initialLink');
  37. if (initialLink != null) initialUri = Uri.parse(initialLink);
  38. } on FormatException catch (e) {
  39. debugPrint("[getInitialLink] FormatException: $e");
  40. initialLink = 'Failed to parse the initial link as Uri.';
  41. initialUri = null;
  42. return ;
  43. } catch (e) {
  44. debugPrint("[getInitialLink] failed: $e");
  45. return ;
  46. }
  47. _latestUri = initialUri;
  48. final queryParams = _latestUri?.queryParametersAll?.entries?.toList();
  49. String pageType;
  50. String id;
  51. if (queryParams != null) {
  52. for (var param in queryParams) {
  53. if (param.key == 'type') {
  54. pageType = param.value.length > 0 ? param.value[0] : null;
  55. }
  56. if (param.key == 'id') {
  57. id = param.value.length > 0 ? param.value[0] : null;
  58. }
  59. }
  60. }
  61. // 这里是根据你自己的 type, id 做相应的跳转页面,下面是个例子
  62. if (pageType == "wares") {
  63. Navigator.pushNamed(context, yourPath, arguments: {'id': id});
  64. }
  65. }
  66. @override
  67. Widget build(BuildContext context) {
  68. // 你自己的 build 函数
  69. }
  70. }

中转页面

一个简单的中转页面:

  1. <!Doctype html>
  2. <html xmlns=http://www.w3.org/1999/xhtml>
  3. <head>
  4. <meta http-equiv=Content-Type content="text/html;charset=utf-8">
  5. <title>Your Title</title>
  6. </head>
  7. <body>
  8. <style>
  9. #mobliestart{font-size:40px;}
  10. </style>
  11. <a href='yourScheme://yourHost?type=report&id=1' id="mobliestart">点击打开APP</a>
  12. <script type="text/javascript">
  13. function applink(){
  14. window.location = 'yourScheme://yourHost?type=report&id=1';
  15. }
  16. applink();
  17. </script>
  18. </body>
  19. </html>

中转页面设置了自动跳转和点击跳转,由于微信浏览器对scheme link的限制,ios和Android都需要点击右上角的‘浏览器中打开’。
跳转到app.jpg