需求: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>