Flutter 跳转到原生Native页面

Flutter 跳转到iOS原生Native页面,flutter 代码进行注册发送调用请求

class _MyHomePageState extends State {
  int _counter = 0;
  MethodChannel eventChannel = const MethodChannel('com.flutterToNative.first');

  void _onPressedAction() {
    try {
      Future future = eventChannel.invokeMethod('firstController');
      print(future.toString());
    } on PlatformException catch (e) {
      print(e.toString());
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
                onPressed: _onPressedAction, child: const Text("跳转到iOS页面")),
          ],
        ),
      ),
    );
  }
}

Flutter 跳转到iOS原生Native页面, iOS 原生需要接收请求,并做进一步处理

@objc class AppDelegate: FlutterAppDelegate {
    
    override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
    guard let contoller = window?.rootViewController as? FlutterViewController else {
        fatalError("contoller is not type FlutterViewController")
    }
    GeneratedPluginRegistrant.register(with: self)
    let navigation = UINavigationController(rootViewController: contoller)
    navigation.setNavigationBarHidden(true, animated: true)
    window.rootViewController = navigation
    flutterSetup()
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }

    func flutterSetup() {
        let navigation = window?.rootViewController as? UINavigationController
        guard let contoller = navigation?.topViewController as? FlutterViewController else {
            fatalError("contoller is not type FlutterViewController")
        }
        let flutterChannel = FlutterMethodChannel(name: "com.flutterToNative.firstt", binaryMessenger: contoller.binaryMessenger)
        flutterChannel.setMethodCallHandler { call, result in
            if call.method == "firstController" {
                let firstView = FirstViewController()
                navigation?.pushViewController(firstView, animated: true)
            } else {
                result(FlutterMethodNotImplemented)
            }
        }
    }

}

你可能感兴趣的:(Flutter,iOS,Swift,flutter,ios)