我想在我们的 Flutter 视图上“推送”一个新的本机视图(Android 中的 Activity 和 iOS 中的 UIViewController),并在新的本机视图完成/完成后,使屏幕返回到我们的 Flutter 视图。
我可以在 Android 中做到这一点。
但我对 iOS 很陌生,当我尝试这样做时ios/Runner/AppDelegate.m
:
SwiftViewController *swiftViewController = [controller.storyboard instantiateViewControllerWithIdentifier:@"SwiftViewController"];
[(FlutterViewController *)self.window.rootViewController pushViewController:swiftViewController animated:YES];
它给出错误:
“FlutterViewController”没有可见的 @interface 声明了选择器“pushViewController:animated”
那么在iOS中如何实现呢?谢谢
在 Swift 中,我们可以使用方法通道从 flutter 操作事件打开本机 iOS viewController,
-
扑腾中,您需要添加以下几行来触发本机iOS方法。在您的操作方法中添加这些行
var platform = const MethodChannel('com.nativeActivity/iosChannel');
final String result = await platform.invokeMethod('StartNativeIOS');
-
In iOS,在AppDelegate应用程序方法中添加以下行
let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
let methodChannel = FlutterMethodChannel(name: "com.nativeActivity/iosChannel", binaryMessenger:controller.binaryMessenger)
methodChannel.setMethodCallHandler({
(call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
if call.method == "StartNativeIOS" {
let mainVC = MainViewController() // Your viewController
let navigationController = UINavigationController(rootViewController: mainVC)
self.window.rootViewController = navigationController
self.window.makeKeyAndVisible()
} else {
result(FlutterMethodNotImplemented)
return
}
})
它会起作用的!干杯
Note:但无法回到 flutter 视图。
所以这是正确的做法。使用协调器模式进行 Flutter 导航 https://medium.com/@cosinus84/flutter-view-controller-used-in-ios-app-using-coordinator-pattern-8896339d64ba
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)