Xcode
創(chuàng)建一個(gè)Swift
項(xiàng)目Swift2FlutterDemo
使用
CocoaPods
安裝Flutter SDK
//初始化CocoaPods
pod init
- 創(chuàng)建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
// 導(dǎo)入Flutter
import Flutter
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
// 創(chuàng)建一個(gè)FlutterEngine對(duì)象
lazy var flutterEngine = FlutterEngine(name: "FlutterEngine")
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// 啟動(dòng)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("跳轉(zhuǎn)", 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)
}
}