在已有ios项目中添加flutter模块,在网上找了许多文章,跟着文章一步步操作没有成功。后来在官方英文文档中找到答案。官方文档官方文档
添加flutter库
第一步
在与ios项目同级的文件下,创建一个flutter model。
flutter create --template module flutter_module
第二步
在podfile文件中,添加如下代码
use_frameworks!
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
#flutter模块路径
flutter_application_path = '../flutter_module'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
target 'Appmix' do
pod 'AFNetworking'
install_all_flutter_pods(flutter_application_path)
end
第三步
pod install
看到pod中有Flutter的相关framework就成功了。
跳转flutter页面
官方提供了两种,一种是AppDelegate继承FlutterAppDelegate来实现,另一种通过AppDelegate实现FlutterAppLifeCycleProvider协议。官方文档
已FlutterAppDelegate为例
现在appdelegate文件中添加如下代码
//AppDelegate.h
#import <UIKit/UIKit.h>
@import Flutter;
@interface AppDelegate : FlutterAppDelegate
@property (nonatomic,strong) FlutterEngine *flutterEngine;
@end
//AppDelegate.m
#import "AppDelegate.h"
#import <FlutterPluginRegistrant/GeneratedPluginRegistrant.h> // Used to connect plugins.
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.flutterEngine = [[FlutterEngine alloc] initWithName:@"io.flutter" project:nil];
[self.flutterEngine runWithEntrypoint:nil];
[GeneratedPluginRegistrant registerWithRegistry:self.flutterEngine];
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
@end
跳转到flutter页面。
-(IBAction)showFlutter{
FlutterEngine *flutterEngine =
((AppDelegate *)UIApplication.sharedApplication.delegate).flutterEngine;
FlutterViewController *flutterViewController =
[[FlutterViewController alloc] initWithEngine:flutterEngine nibName:nil bundle:nil];
flutterViewController.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:flutterViewController animated:YES completion:nil];
}