flutter deeplink 跳转 + swift dart 交互

需求:flutter app 需要做deeplink 从h5点击跳转到 app 指定页面,可以用schme来实现。
1、在xcode 配置schme url
在这里插入图片描述

2、在appDelegate.swift里配置跳转代码

override func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        if (url.relativeString.contains("moegirl://")){
            let controller:FlutterViewController = self.window.rootViewController as! FlutterViewController
            let jumpChannel = FlutterMethodChannel.init(name: "channel:moegirlJump", binaryMessenger: controller  as! FlutterBinaryMessenger)
            jumpChannel.invokeMethod("moegirlJump", arguments: url.relativeString)
            return true;
        }
    }

3、在flutter首页注册iOS调用dart的方法并实现跳转

//注册iOS调用flutter方法
    const channel = MethodChannel('channel:moegirlJump');
    channel.setMethodCallHandler((MethodCall call) async {
      if (call.method == 'moegirlJump') {
        Logger().d("call.arguments = ${call.arguments}");
        return (schemeJump(context, call.arguments));
      }
    });
/// 路由跳转 context schemeUrl
void schemeJump(BuildContext context, String schemeUrl) {
  final _jumpUri = Uri.parse(schemeUrl.replaceFirst(
    'moegirl://',
    'http://path/',
  ));
  switch (_jumpUri.path) {
    case '/detail':
      moegirlRouter(
          context,
          Platform.isIOS,
          EntryDetailsPage(
            entryId: _jumpUri.queryParameters['id'] != null
                ? int.parse(_jumpUri.queryParameters['id'])
                : 0,
            entryTitle: _jumpUri.queryParameters['title'] ?? '暂无条目',
            entryWebUrl: _jumpUri.queryParameters['url'],
          ),
          CartoonRouter.CustomUpRouteSlide);
      break;
    default:
      break;
  }
}

5、可以在github上部署一个h5以供测试
在这里插入图片描述

<!doctype html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Scheme 跳转</title>
</head>
<style>
    h3,
    p {
        text-align: center;
    }
</style>

<body>
    <h3>
        <a href="moegirl://homepage?">
            打开App(moegirl://homepage?)
        </a>
    </h3>
    <p>moegirl://homepage?</p>
    <h3>
        <a href="moegirl://detail?id=322786&title=任天堂&url=https://mobile.moegirl.org.cn/任天堂">
            打开App跳转到详情页面
        </a>
    </h3>
    <p>moegirl://detail?id=322786&title=任天堂&url=https://mobile.moegirl.org.cn/任天堂</p>
    <h3>
        <a href="https://zh.moegirl.org.cn/Mainpage">
            iOS universallink跳转
        </a>
    </h3>
    <p>iOS universallink跳转</p>
</body>

</html>

测试链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值