如何使用特定于平台的代码将数据从 Flutter 传递到本机 iOS (Swift)。我在互联网上搜索和堆栈溢出,但它只显示了一个原生 Android 的例子。以下是到目前为止我尝试过的代码
飞镖代码
static const methodChannel = MethodChannel('com.ios');
void openPaymentMethod() async {
String? result = await methodChannel
.invokeMethod<String>('isSetUpAvailable', {'text': hello});
if (result != null) {
print(result);
}
}
本机 Swift 代码
let METHOD_CHANNEL_NAME = "com.ios"
let controller: FlutterViewController = window?.rootViewController as! FlutterViewController
let methodChannel = FlutterMethodChannel(name: METHOD_CHANNEL_NAME, binaryMessenger: controller.binaryMessenger)
methodChannel.setMethodCallHandler({
(call:FlutterMethodCall,result: @escaping FlutterResult) -> Void in
switch call.method{
case "isSetUpAvailable":
result("Success")
default:
result(FlutterMethodNotImplemented)
}
})