Learn how to make a Swift native call from Flutter

In this tutorial, we are going to call a simple swift method of AppDelegate.swift file from flutter.

Let’s create a brand new flutter app by running the following command on your terminal

flutter create ios_native

Delete everything inside the method _incrementCounter() on your main.dart file and make it an async method.

 Future<void> _incrementCounter() async {
    const MethodChannel _channel =
        const MethodChannel("FlutterFramework/swift_native");
    final result = await _channel.invokeMethod('getSum');
    print("result from iOS native + Swift ${result}");

Now open your Runner.xcworkspace on Xcode and open AppDelegate.swift

private func getSum() -> Int {
    return 2 + 2

Add the following code in the method didFinishLaunchingWithOptions and close the Xcode.

 let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
      let channel = FlutterMethodChannel(name: "FlutterFramework/swift_native",
                                         binaryMessenger: controller.binaryMessenger)
          [weak self]  (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
          // This method is invoked on the UI thread.
          // Handle battery messages.
          guard call.method == "getSum" else {

Now run the flutter app on the VSCode or Android Studio, if you click the floating button you will swift method returns 2 +2 = 4


Call native Android code from Flutter

Spread the love

Leave a comment