iOS现有项目集成Flutter

一、创建Flutter模块

1.cd ~/Work/Projects
2.flutter create -t module flutter_module

image.png

二、创建依赖

1.通过CocoaPods管理依赖库,直接在 iOS 工程的Podfile输入

platform:ios,'9.0'
target 'FlutterModuleiOS' do

use_frameworks!

end

flutter_application_path = '../flutter_module'
eval(File.read(File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')), binding)

2.遇到的问题

image.png

flutter_application_path = '../flutter_module' //写入的模块名称不正确

3. Enable Bitcode为NO

image.png

4. 在Xcode工程中添加Dart代码的build phase

image.png
"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" build
"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" embed

5.遇到的问题

image.png

image.png

三、在iOS项目中,通过FlutterViewController跳转至Flutter页面

AppDelegate.h/m
AppDelegate.h

#import 
#import 

@interface AppDelegate : FlutterAppDelegate

@end

AppDelegate.m

#import "AppDelegate.h"

@interface AppDelegate () @end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

@end

ViewController.h/m
ViewController.m

#import "ViewController.h"
#import 
#import  // Only if you have Flutter Plugins

@interface ViewController ()

@end

@implementation ViewController
    
- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button addTarget:self action:@selector(handleButtonAction)
     forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"点击" forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor redColor]];
    button.frame = CGRectMake(30, 0, [UIScreen mainScreen].bounds.size.width - 60, 40.0);
    button.center = self.view.center;
    [self.view addSubview:button];
}
    
- (void)handleButtonAction {
    FlutterViewController *flutterViewController = [[FlutterViewController alloc] init];
    flutterViewController.view.backgroundColor = [UIColor cyanColor];
    [flutterViewController setInitialRoute:@"route1"];
    
    [GeneratedPluginRegistrant registerWithRegistry:flutterViewController];
    
    [self presentViewController:flutterViewController animated:YES completion:nil];
}

@end

你可能感兴趣的:(iOS现有项目集成Flutter)