Flutter 開發(fā)集成

一忆畅、Flutter 安裝與配置

1.搭建Flutter - iOS開發(fā)環(huán)境

  • 克隆Flutter到Library目錄
sudo git clone https://github.com/flutter/flutter.git $HOME/Library/flutter

2.配置Flutter環(huán)境變量

  • 為了方便后續(xù)使用站宗,需要將項目根目錄下bin路徑加入環(huán)境變量PATH
    *執(zhí)行命令open ~/.bash_profile在底部添加環(huán)境變量护赊。
# Flutter 相關(guān)配置
export PATH=$HOME/Library/flutter/bin:$PATH
export FLUTTER_ROOT=$HOME/Library/flutter

// 國內(nèi)臨時鏡像地址
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
  • 然后生效環(huán)境變量,終端 執(zhí)行 source ~/.bash_profile
  • 驗證flutter/bin目錄是否在你的PATH
# 執(zhí)行下面命令
echo $PATH
  • 如果安裝成功, 會輸出類似/xxx/flutter/bin:的路徑

3.環(huán)境配置檢測

  • 在進行配置前, 首先需要安裝CocoaPods
  • 安裝CocoaPods后, 在執(zhí)行sudo flutter doctor命令, 可能會出現(xiàn)如下問題
$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[?] Flutter (Channel master, v1.2.1-pre.197, on Mac OS X 10.14.3 18D109, locale
    zh-Hans)
[?] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
[!] iOS toolchain - develop for iOS devices (Xcode 10.1)
    ? Verify that all connected devices have been paired with this computer in
      Xcode.
      If all devices have been paired, libimobiledevice and ideviceinstaller may
      require updating.
      To update with Brew, run:
        brew update
        brew uninstall --ignore-dependencies libimobiledevice
        brew uninstall --ignore-dependencies usbmuxd
        brew install --HEAD usbmuxd
        brew unlink usbmuxd
        brew link usbmuxd
        brew install --HEAD libimobiledevice
        brew install ideviceinstaller
    ? CocoaPods not installed.
        CocoaPods is used to retrieve the iOS platform side's plugin code that
        responds to your plugin usage on the Dart side.
        Without resolving iOS dependencies with CocoaPods, plugins will not work
        on iOS.
        For more info, see https://flutter.io/platform-plugins
      To install:
        brew install cocoapods
        pod setup
[?] Android Studio (version 3.2)
[!] Connected device
    ! No devices available

! Doctor found issues in 2 categories.
  • 此時在終端中依次執(zhí)行出現(xiàn)的命令即可
        [!] iOS toolchain - develop for iOS devices
        brew update
        brew uninstall --ignore-dependencies libimobiledevice
        brew uninstall --ignore-dependencies usbmuxd
        brew install --HEAD usbmuxd
        brew unlink usbmuxd
        brew link usbmuxd
        brew install --HEAD libimobiledevice
        brew install ideviceinstaller
  • 配置完后,此刻再運行flutter doctor命令

如果重復(fù)提示 iOS toolchain - develop for iOS devices鳖孤,進入對應(yīng)目錄刪除libimobiledevice习瑰、usbmuxdideviceinstaller庫绪颖,重啟電腦即可

Doctor summary (to see all details, run flutter doctor -v):
[?] Flutter (Channel stable, v1.0.0, on Mac OS X 10.14.3 18D109, locale
    zh-Hans-CN)
[?] Android toolchain - develop for Android devices (Android SDK 28.0.3)
[?] iOS toolchain - develop for iOS devices (Xcode 10.1)
[?] Android Studio (version 3.2)
[?] Connected device (1 available)

? No issues found!

二、已有項目集成Flutter

1.創(chuàng)建Flutter應(yīng)用

  • 首先cd到flutter工程同級目錄甜奄,執(zhí)行flutter命令
flutter create -t module flutter_module

2.創(chuàng)建iOS的銜接文件

  • 打開iOS工程柠横,在Xcod中點擊Flie->New->Flie添加Configuration Settings File文件,命名為FlutterConfig.xcconfig

注意添加的路徑為FlutterDemo/flutter_module课兄,添加完成后牍氛,此時Xcode工程目錄下會多一個FlutterConfig.xcconfig文件引用,可刪除此引用

  • Flutte添加xcconfig文件引用烟阐,在FlutterConfig.xcconfig里添加 #include "./.ios/Flutter/Generated.xcconfig"引用flutter_module下的ios插件里的Generated.xcconfig文件
  • iOS工程添加文件引用搬俊,創(chuàng)建Debug.xcconfigRelease.xcconfig蜒茄,然后將對應(yīng)的xcconfig添加到iOS項目的Info-Configuration里唉擂。代碼如下
// FlutterConfig.xcconfig
#include "./.ios/Flutter/Generated.xcconfig"
ENABLE_BITCODE=NO

// Debug.xcconfig
#include "../.././flutter_module/FlutterConfig.xcconfig"

//  Release.xcconfig
#include "../.././flutter_module/FlutterConfig.xcconfig"
FLUTTER_BUILD_MODE=release

3.引入xcode-backend.sh

  • iOS工程里添加運行 Flutter 的腳本,并且確保Run Script這一行在 Target dependencies 或者 Check Pods Manifest.lock后面

判斷腳本路徑是否正確檀葛,可以執(zhí)行指令open $FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh

4.引入Flutter編譯產(chǎn)物

  • 編譯完項目玩祟,把flutter_module/.ios/Flutter下的App.frameworkFlutter.framework屿聋、flutter_assets添加進項目空扎,確保文件夾下的兩個framework添加到Embeded Binaries

注意:flutter_assets 并不能使用Create groups 的方式添加,只能使用 Creat folder references的方式添加進Xcode項目內(nèi)

5.iOS工程改造

  • AppDelegate.swift改造
import Flutter

@UIApplicationMain
class AppDelegate: FlutterAppDelegate {

   var flutterEngine : FlutterEngine?
    
    override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        self.flutterEngine = FlutterEngine(name: "io.flutter", project: nil)
        self.flutterEngine?.run(withEntrypoint: nil)
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
}
  • 進入flutter頁面
//
//  ViewController.swift
//  EZBuy-flutter
//
//  Created by qiuming on 2019/2/15.
//  Copyright ? 2019 qiuming. All rights reserved.
//

import UIKit
import Flutter

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func handleButtonAction(_ sender: UIButton) {
        let flutterEngine = (UIApplication.shared.delegate as? AppDelegate)?.flutterEngine;
        let flutterViewController = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)!;
        self.present(flutterViewController, animated: false, completion: nil)
    }
}

三润讥、cocoapods集成

對于手動集成转锈,其實發(fā)現(xiàn)只需要集成Flutter 核心編譯產(chǎn)物,項目就能接入Flutter 的功能象对。
App.frameworkdart業(yè)務(wù)源碼相關(guān)文件黑忱,在 Debug 模式下就是一個很小的空殼,在 Release 模式下包含全部業(yè)務(wù)邏輯。
flutter_assetsFlutter依賴的靜態(tài)資源甫煞,如字體菇曲,圖片等。
Flutter.frameworkFlutter庫和引擎抚吠。

  • podspec制作常潮,校驗通過后上傳至cocoapods
#
#  Be sure to run `pod spec lint EZFlutterSDK.podspec' to ensure this is a
#  valid spec and to remove all comments including this before submitting the spec.
#
#  To learn more about Podspec attributes see https://docs.cocoapods.org/specification.html
#  To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/
#

Pod::Spec.new do |spec|

  spec.name         = "EZFlutterSDK"
  spec.version      = "0.0.1"
  spec.summary      = "Flutter編譯后的文件"
  spec.homepage     = "https://github.com/wutongyu008/FlutterSDK"
  spec.license      = { :type => "MIT", :file => "LICENSE" }
  spec.author       = { "wutongyu008" => "wutongyu_08@163.com" }
  spec.platform     = :ios, "9.0"
  spec.source       = { :git => "https://github.com/wutongyu008/FlutterSDK.git", :tag => "#{spec.version}" }

  spec.vendored_frameworks = "Framework/*.framework", "Framework/engine/*.framework"
  spec.resources = "Framework/flutter_assets"

end
  • 私有庫制作好后,在Podfile中使用私有庫
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target 'Flutter-IOS' do
  use_frameworks!

  pod 'EZFlutterSDK', '~> 0.0.1'

  target 'Flutter-IOSTests' do
    inherit! :search_paths
  end

end

按上文重新設(shè)置一下AppDelegate和需要使用FlutterViewController即可
demo地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末楷力,一起剝皮案震驚了整個濱河市喊式,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌萧朝,老刑警劉巖岔留,帶你破解...
    沈念sama閱讀 221,198評論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異检柬,居然都是意外死亡献联,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評論 3 398
  • 文/潘曉璐 我一進店門何址,熙熙樓的掌柜王于貴愁眉苦臉地迎上來里逆,“玉大人,你說我怎么就攤上這事用爪≡海” “怎么了?”我有些...
    開封第一講書人閱讀 167,643評論 0 360
  • 文/不壞的土叔 我叫張陵偎血,是天一觀的道長诸衔。 經(jīng)常有香客問我,道長烁巫,這世上最難降的妖魔是什么署隘? 我笑而不...
    開封第一講書人閱讀 59,495評論 1 296
  • 正文 為了忘掉前任宠能,我火速辦了婚禮亚隙,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘违崇。我一直安慰自己阿弃,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,502評論 6 397
  • 文/花漫 我一把揭開白布羞延。 她就那樣靜靜地躺著渣淳,像睡著了一般。 火紅的嫁衣襯著肌膚如雪伴箩。 梳的紋絲不亂的頭發(fā)上入愧,一...
    開封第一講書人閱讀 52,156評論 1 308
  • 那天,我揣著相機與錄音,去河邊找鬼棺蛛。 笑死怔蚌,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的旁赊。 我是一名探鬼主播桦踊,決...
    沈念sama閱讀 40,743評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼终畅!你這毒婦竟也來了籍胯?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,659評論 0 276
  • 序言:老撾萬榮一對情侶失蹤离福,失蹤者是張志新(化名)和其女友劉穎杖狼,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體妖爷,經(jīng)...
    沈念sama閱讀 46,200評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡本刽,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,282評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了赠涮。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片子寓。...
    茶點故事閱讀 40,424評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖笋除,靈堂內(nèi)的尸體忽然破棺而出斜友,到底是詐尸還是另有隱情,我是刑警寧澤垃它,帶...
    沈念sama閱讀 36,107評論 5 349
  • 正文 年R本政府宣布鲜屏,位于F島的核電站,受9級特大地震影響国拇,放射性物質(zhì)發(fā)生泄漏洛史。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,789評論 3 333
  • 文/蒙蒙 一酱吝、第九天 我趴在偏房一處隱蔽的房頂上張望也殖。 院中可真熱鬧,春花似錦务热、人聲如沸忆嗜。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽捆毫。三九已至,卻和暖如春冲甘,著一層夾襖步出監(jiān)牢的瞬間绩卤,已是汗流浹背途样。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評論 1 271
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留濒憋,地道東北人娘纷。 一個月前我還...
    沈念sama閱讀 48,798評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像跋炕,于是被迫代替她去往敵國和親赖晶。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,435評論 2 359

推薦閱讀更多精彩內(nèi)容