Pass data from flutter to native IOS

3.1k Views Asked by At

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)
      }
  })
1

There are 1 best solutions below

0
Mohammed Nabil On

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