【Flutter】iOS原生项目接入01
Flutter官网:Adding Flutter to iOS
项目结构:
Flutter模块的创建路径 务必 在 xxx.xcodeproj工程文件的上一级目录
│ occ/project/
│ ────└── test_flutter/
│ ────└── MyApp/
│ ──────── └── Podfile
flutter_app_path = '../test_flutter'
1、创建flutter项目(项目名要小写)
cd occ/project/
flutter create --template module test_flutter
2、在原生项目引入,然后执行pod install即可引入
在podfile文件中添加以下代码:
#flutter项目的相对路径
flutter_app_path = '../test_flutter'
load File.join(flutter_app_path, '.ios', 'Flutter', 'podhelper.rb')
target 'MyApp' do
install_all_flutter_pods(flutter_app_path)
end
3、测试,
注意启动类调整:class AppDelegate: FlutterAppDelegate {
import UIKit
import Flutter
import FlutterPluginRegistrant
import SwiftUI
@main
class AppDelegate: FlutterAppDelegate{
lazy var flutterEngine = FlutterEngine(name: "Flutter Demo")
override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Runs the default Dart entrypoint with a default Flutter route.
flutterEngine.run();
// Used to connect plugins (only if you have plugins with iOS platform code).
GeneratedPluginRegistrant.register(with: self.flutterEngine);
let mainTab : CCMainTabbarCtrl = CCMainTabbarCtrl.init()
self.window = UIWindow(frame: UIScreen.main.bounds);
self.window?.backgroundColor = UIColor.white;
self.window?.rootViewController = mainTab;
self.window?.makeKeyAndVisible()
return super.application(application, didFinishLaunchingWithOptions: launchOptions);
}
}
import UIKit
import Flutter
class CCMainHomeCtrl: CCBaseCtrl {
override func viewDidLoad() {
super.viewDidLoad()
// Make a button to call the showFlutter function when pressed.
let button = UIButton(type:UIButton.ButtonType.custom)
button.addTarget(self, action: #selector(showFlutter), for: .touchUpInside)
button.setTitle("Show Flutter!", for: UIControl.State.normal)
button.frame = CGRect(x: 80.0, y: 210.0, width: 160.0, height: 40.0)
button.backgroundColor = UIColor.blue
self.view.addSubview(button)
}
@objc func showFlutter() {
let flutterEngine = (UIApplication.shared.delegate as! AppDelegate).flutterEngine
let flutterViewController = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)
//present(flutterViewController, animated: true, completion: nil)
self.navigationController?.pushViewController(flutterViewController, animated: true)
}
}
运行错误提示:
Failed to find assets path for "Frameworks/App.framework/flutter_assets"
Engine run configuration was invalid
Could not launch engine with configuration.
添加脚本
target
->Build Phases
->+
->New Run Script Phase
->Run Script
"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" build
"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" embed ${SOURCE_ROOT}/Flutter/App.framework
截屏.png
运行错误提示:
line 2: /packages/flutter_tools/bin/xcode_backend.sh: No such file or directory
line 3: /packages/flutter_tools/bin/xcode_backend.sh: No such file or directory
检查FLUTTER_ROOT路径是否对,如若没有,添加自己安装flutter的位置路径
target
->Build Settings
(若没有搜索到FLUTTER_ROOT)->+
->Add User-Defined Setting