Xcode
创建一个Swift
项目Swift2FlutterDemo
使用
CocoaPods
安装Flutter SDK
//初始化CocoaPods
pod init
- 创建Flutter模块
flutter create --template module my_flutter
- 编写
Podfile
文件
#添加模块所在的路径
flutter_application_path = './my_flutter'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
target 'Swift2FlutterDemo' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# Pods for Swift2FlutterDemo
#安装Flutter模块
install_all_flutter_pods(flutter_application_path)
end
post_install do |installer|
flutter_post_install(installer)
end
-
CocoaPods
安装依赖
pod install
- 示例代码
import UIKit
// 导入Flutter
import Flutter
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
// 创建一个FlutterEngine对象
lazy var flutterEngine = FlutterEngine(name: "FlutterEngine")
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// 启动FlutterEngine
flutterEngine.run()
return true
}
}
import UIKit
import Flutter
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let button = UIButton(type: .custom)
button.frame = CGRect(x: 100, y: 100, width: 100, height: 50)
button.backgroundColor = UIColor.red
button.setTitle("跳转", for: .normal)
button.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
self.view.addSubview(button)
}
@objc func buttonClick() {
let flutterEngine = (UIApplication.shared.delegate as! AppDelegate).flutterEngine
let flutterViewController = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)
self.present(flutterViewController, animated: true)
}
}