Flutter 开发集成

一、Flutter 安装与配置

1.搭建Flutter - iOS开发环境

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

2.配置Flutter环境变量

  • 为了方便后续使用,需要将项目根目录下bin路径加入环境变量PATH
    *执行命令open ~/.bash_profile在底部添加环境变量。
# Flutter 相关配置
export PATH=$HOME/Library/flutter/bin:$PATH
export FLUTTER_ROOT=$HOME/Library/flutter

// 国内临时镜像地址
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
  • 然后生效环境变量,终端 执行 source ~/.bash_profile
  • 验证flutter/bin目录是否在你的PATH
# 执行下面命令
echo $PATH
  • 如果安装成功, 会输出类似/xxx/flutter/bin:的路径

3.环境配置检测

  • 在进行配置前, 首先需要安装CocoaPods
  • 安装CocoaPods后, 在执行sudo flutter doctor命令, 可能会出现如下问题
$ 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.
  • 此时在终端中依次执行出现的命令即可
        [!] 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命令

如果重复提示 iOS toolchain - develop for iOS devices,进入对应目录删除libimobiledeviceusbmuxdideviceinstaller库,重启电脑即可

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.创建Flutter应用

  • 首先cd到flutter工程同级目录,执行flutter命令
flutter create -t module flutter_module

2.创建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工程添加文件引用,创建Debug.xcconfigRelease.xcconfig,然后将对应的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后面

判断脚本路径是否正确,可以执行指令open $FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh

4.引入Flutter编译产物

  • 编译完项目,把flutter_module/.ios/Flutter下的App.frameworkFlutter.frameworkflutter_assets添加进项目,确保文件夹下的两个framework添加到Embeded Binaries

注意:flutter_assets 并不能使用Create groups 的方式添加,只能使用 Creat folder references的方式添加进Xcode项目内

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集成

对于手动集成,其实发现只需要集成Flutter 核心编译产物,项目就能接入Flutter 的功能。
App.frameworkdart业务源码相关文件,在 Debug 模式下就是一个很小的空壳,在 Release 模式下包含全部业务逻辑。
flutter_assetsFlutter依赖的静态资源,如字体,图片等。
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" => "[email protected]" }
  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

按上文重新设置一下AppDelegate和需要使用FlutterViewController即可
demo地址

你可能感兴趣的:(Flutter 开发集成)