Flutter官網(wǎng):Adding Flutter to iOS
項(xiàng)目結(jié)構(gòu):
Flutter模塊的創(chuàng)建路徑 務(wù)必 在 xxx.xcodeproj工程文件的上一級目錄
│ occ/project/
│ ────└── test_flutter/
│ ────└── MyApp/
│ ──────── └── Podfile
flutter_app_path = '../test_flutter'
1卷拘、創(chuàng)建flutter項(xiàng)目(項(xiàng)目名要小寫)
cd occ/project/
flutter create --template module test_flutter
2堕阔、在原生項(xiàng)目引入谴供,然后執(zhí)行pod install即可引入
在podfile文件中添加以下代碼:
#flutter項(xiàng)目的相對路徑
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媒至、測試咙边,
注意啟動類調(diào)整: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)
}
}
運(yùn)行錯(cuò)誤提示:
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
運(yùn)行錯(cuò)誤提示:
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