【发布时间】:2020-05-21 17:33:08
【问题描述】:
我正在尝试从我们的原生 iOS 应用程序中打开两个不同的颤振模块,该应用程序具有许多用 swift 编写的功能并且可以正常工作。现在我们要在Flutter 中构建两个新功能。为此,我们需要从原生 iOS 代码中打开两个不同的 Flutter 模块或方法。
我尝试了很多解决方案,但都没有运气。以下是我创建的两个示例颤振方法,它们将来会触发两个不同功能的路由:
//First Method
class MyFirstApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Welcome to Flutter',
home: Scaffold(
appBar: AppBar(
title: Text('Welcome to My First App'),
),
body: Center(
child: Text('Hello World 1'),
),
),
);
}
}
//Second Method
class MySecondApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Welcome to Flutter',
home: Scaffold(
appBar: AppBar(
title: Text('Welcome to My Second App'),
),
body: Center(
child: Text('Hello World 2'),
),
),
);
}
}
这是我尝试用flutter写的route:
class RouteState extends StatefulWidget {
RouteState({Key key, this.title}) : super(key: key);
final String title;
@override
_RouteState createState() => _RouteState();
}
class _RouteState extends State<RouteState> {
static const apiChannel = const MethodChannel('my_channel');
Future<dynamic> handleMethod(MethodCall call) async {
switch(call.method) {
case 'first':
return MyFirstApp();
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Text('Hello World 0'),
),
);
}
}
现在从 iOS 开始,我尝试在原生代码库的 viewcontroller 中打开上述任何一种方法。
let flutterEngine = (UIApplication.shared.delegate as! AppDelegate).flutterEngine
let flutterViewController = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)
let channel = FlutterMethodChannel(name: "my_channel", binaryMessenger: flutterViewController.binaryMessenger)
channel.invokeMethod("first", arguments: "")
//viewController.show(flutterViewController, sender: viewController)
但是什么都没有发生,没有从这里打开任何东西。也不会为任何错误打印日志。我在双方都进行了调试,断点到达了 iOS 端的所有代码,而在颤动端,一切都很好。当我只运行带有main() 的颤振模块时,它起作用了。但不是从原生打开。
【问题讨论】:
标签: ios flutter module communication