1) 创建flutter module
1) 将iOS项目放在一个新建的文件夹里面
2) cd到新建的文件夹
3) 执行
$ flutter create -t module flutter_module
会在项目同级目录下生成一个flutter_module文件夹
2) 导入flutter库
1) 在项目的Podfile文件里添加以下内容(如果iOS项目里面没用cocoapod那么新建一个Podfile文件)
flutter_application_path = '../flutter_module'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
install_all_flutter_pods(flutter_application_path)
2) 执行
$ pod install
可以看到导入相关的Flutter库
3) AppDelegate 配置
1) 导入 Flutter 和 FlutterPluginRegistrant
2) AppDelegate继承FlutterAppDelegate
3) 申明一个FlutterEngine类型的属性
4) 初始化engine
具体配置如下
import Flutter
import FlutterPluginRegistrant
class AppDelegate: FlutterAppDelegate {
var flutterEngine:FlutterEngine?
override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.backgroundColor = UIColor.white
window?.makeKeyAndVisible()
window?.rootViewController = UIViewController()
self.flutterEngine = FlutterEngine(name:"io.flutter", project: nil)
self.flutterEngine?.run(withEntrypoint: nil)
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
4) 页面跳转
if let flutterEngine =(UIApplication.shared.delegate as? AppDelegate)?.flutterEngine {
let vc = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)
navigationController?.pushViewController(vc, animated: true)
}
网友评论