iOS原生混编flutter项目配置

先用Xcode创建一个原生项目,例如命名为TestFlutter

原生项目

在项目的根目录执行创建flutter模块的命令

flutter create -t module 模块名
例如:
flutter create -t module test_flutter_module
test_flutter_module模块和原生项目,注意它们在同级目录

安装cocapods,在项目根目录创建podflile文件,然后修改podfile,然后执行pod install 命令

platform:ios, '9.0'

target 'TestFlutter' do
pod 'MBProgressHUD'

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

end

修改原生项目的工程配置:
点击.xcworkSpace文件打开iOS工程,找到Build Phases目录,新建一个Script Phase

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

2

然后cmd+B快捷键 xcode编译一下项目,如果编译成功,则配置完成

测试原生调用flutter

在原生iOS项目中,修改AppDelegate.h文件,使AppDelegate 继承自FlutterAppDelegate;修改AppDelegate.m的方法实现

AppDelegate.h

#import 
#import 

@interface AppDelegate : FlutterAppDelegate 

@property (strong, nonatomic) UIWindow *window;

@end

AppDelegate.m

#import "AppDelegate.h"
#import 

@interface AppDelegate ()
@end

@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [GeneratedPluginRegistrant registerWithRegistry:self];
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
@end

修改原生项目ViewController.m文件代码

#import "ViewController.h"
#import 

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    FlutterViewController* flutterViewController = [[FlutterViewController alloc] init];
    [self presentViewController:flutterViewController animated:YES completion:nil];
}
@end

然后使用Xcode 快捷键cmd+R 运行后,点击屏幕即可跳转到flutter页面

你可能感兴趣的:(iOS原生混编flutter项目配置)