SoFunction
Updated on 2025-04-09

How to integrate Flutter into existing iOS projects

Preface

I have written an article introducing flutter integration into Android projects. This time I will summarize and record the process of integrating flutter into iOS, as well as the problems encountered and solutions for your reference.

Create flutter_module

To integrate flutter in iOS project, we first need to create a flutter_module. There are two ways to create it:

Created with Android studio
Created with Android studio in the previous postThe new version of Flutter is integrated into existing Android projectsThere is an introduction in this, and I will not repeat it here.

Create using flutter command
Execute the following command in the specified directory

flutter create --template module flutter_module

Modify the Podfile file

Add the following content in the dependencies section of the Podfile file

flutter_application_path'/Users/liuxinye/Desktop/WorkPro/AppGroup/platforms/xagf_flutter'
load (flutter_application_path, '.ios', 'Flutter', '')

Then add in the target you want to integrate flutter

install_all_flutter_pods(flutter_application_path)

Finally, the integration of flutter_module is completed by executing pod install for iOS project.

Using flutter

Next, we introduce how to enable flutter for development in iOS projects

Initialize FlutterEngine

First we need to hold a FlutterEngine in AppDelegate, the code is as follows:

@import Flutter
@interface AppDelegate
@property (strong, nonatomic) FlutterEngine *flutterEngine;
@end

Then we need to start FlutterEngine at the right time

+(FlutterEngine *)initFlutter:(NSString*)entry :(NSString*)dartFile{
   FlutterEngine *flutterEngine =
     ((AppDelegate *)).flutterEngine;
  if (flutterEngine) {
    return flutterEngine;
  }else{
    flutterEngine = [[FlutterEngine alloc] initWithName:@"my flutter engine"];
    if(dartFile&&entry){
      //Specify the entry method in the dartFile file to start flutter      [flutterEngine runWithEntrypoint:entry libraryURI:dartFile];
    }else if (entry){
      //Specify the method named entry method starts      [flutterEngine runWithEntrypoint:entry];
    }else{
      //Execute the main method in the default file to start      [flutterEngine run];
    }
    return flutterEngine;
  }
}

Here are three ways to start Flutter, and the differences between the three methods are commented in the code.

Of course, we can also initialize the route when starting flutter.

[[flutterEngine navigationChannel] invokeMethod:@"setInitialRoute"
                     arguments:@"/index"];

At this point, we have completed the startup of FlutterEngine. Next, we will create a ViewContoller used to display the Flutter interface.

Create a FlutterViewController

[GeneratedPluginRegistrant registerWithRegistry:flutterEngine];
FlutterViewController *flutterViewController =
    [[FlutterViewController alloc] initWithEngine:flutterEngine nibName:nil bundle:nil];

Use it as a UIView

If you want to use it as a UIView, you only need to use it UIView *myFlutterView= Just do it.

Problems encountered

The integration of Flutter to iOS project was quite smooth. I only encountered one problem during the integration process. After completing the above integration work, I ran the iOS project and found an error that lib/ could not be found. I couldn't solve it on Baidu for a long time. Finally, I tried to close xcode, delete the Pods folder and file and re-execute pod install to solve this problem.

This is the article about the methods and steps of integrating Flutter into existing iOS projects. For more related content related to integration of Flutter into existing iOS, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!