2

How to pass data from flutter to native iOS (Swift) by using platform specific code. I searched in the internet and stack overflow but it is showing an example of only Native Android. Below is the code till now what i have tried

Dart code

static const methodChannel = MethodChannel('com.ios');
void openPaymentMethod() async {
        String? result = await methodChannel
            .invokeMethod<String>('isSetUpAvailable', {'text': hello});
        if (result != null) {
          print(result);
        }
      }

Native Swift code

  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)
      }
  })
Mohammed Nabil
  • 662
  • 1
  • 8
  • 36
  • 1
    Please see: https://stackoverflow.com/questions/57664458/how-to-use-passed-parameters-in-swift-setmethodcallhandler-self-methodnamere/57666911#57666911 and https://stackoverflow.com/questions/61489762/how-to-call-arguments-from-flutter-in-swift-native-code/61505679#61505679 – Richard Heap Dec 05 '21 at 13:47

1 Answers1

2

I Solved this by using this code

guard let result = call.arguments else {
                  return
              }
              let myresult = result as? [String: Any]
              let data = myresult?["text"] as? String
Mohammed Nabil
  • 662
  • 1
  • 8
  • 36