Jan 30, 2024

Integrating Flutter into a Native iOS Application

Learn how Flutter can be integrated into a native iOS app written in Swift from Aswin Gopinathan, Software Engineer at PhonePe.
ChayanTechnical Content Writer

Flutter is a popular cross-platform UI framework that allows developers to build mobile apps for iOS and Android from a single codebase. While Flutter provides a full-stack solution for building mobile apps, it can also be integrated into existing native apps as a module.

In this post, we'll look at how to integrate Flutter into a native iOS app written in Swift.

Setting Up the Project

To get started, we'll first create a new native iOS project using Xcode and Swift. I named my project "AddToAppDemo".

Next, we'll create a new Flutter module. I used Visual Studio Code for this and selected the "module" project type when creating the new Flutter project. I named the Flutter module "add_to_app_demo" and placed it within the same parent folder as the iOS project.

Now we have a native iOS project and a Flutter module within the same folder structure.

Connecting the iOS and Flutter Projects

To connect the two projects, we first need to configure the iOS project's podfile to import the Flutter module.

The key steps are:

  1. Specify the path to the Flutter module
  2. Specify the path to the Flutter podhelper file
  3. Add install_all_flutter_pods to install the required Flutter packages

After updating the podfile, run pod install to install the Flutter dependencies.

Now the iOS project is ready to load Flutter views.

Loading a Flutter Screen

Within the iOS code, we first need to create an instance of FlutterEngine and start it by calling run() on the engine. This initializes the Flutter environment.

When we want to show a Flutter screen, such as when tapping a button, we:

  1. Get the FlutterEngine instance
  2. Create a FlutterViewController using the engine
  3. Present the Flutter view controller using the navigation controller

This will load the Flutter UI from the module into the iOS app.

And that's the core workflow for integrating Flutter into a native iOS app! Flutter provides platform-specific build artifacts that can be embedded within the native project.

Additional Tips

A few other tips:

  • Can only import one Flutter module directly. To add multiple, create an entry point module that imports the others.
  • Flutter provides an imperative, declarative UI approach, while iOS uses an imperative approach.
  • Focus on becoming a great mobile developer rather than just a Flutter developer.

By following these steps, developers can leverage the power of Flutter while maintaining the flexibility and functionality of their existing iOS projects.

Watch the full address here 👇🏻

Hire our Development experts.