将Flutter添加到现有IOS项目

2019-08-29  本文已影响0人  一个半吊子工程师

Cocoapods版本 1.9.0

pod --version

1.创建一个Flutter模块

假设您有一个现有的iOS应用程序some/path/MyApp,并且您希望将Flutter项目作为兄弟

$ cd some/path/

$ flutter create -t module my_flutter

这将创建一个some/path/my_flutter/Flutter模块项目,其中包含一些Dart代码以帮助您入门,以及一个.ios/隐藏的子文件夹,它包含了包含一些Cocoapods和一个帮助程序Ruby脚本的模块项目。

podhelper.rb//附件
def parse_KV_file(file, separator='=')
    file_abs_path = File.expand_path(file)
    if !File.exists? file_abs_path
        return [];
    end
    pods_array = []
    skip_line_start_symbols = ["#", "/"]
    File.foreach(file_abs_path) { |line|
        next if skip_line_start_symbols.any? { |symbol| line =~ /^\s*#{symbol}/ }
        plugin = line.split(pattern=separator)
        if plugin.length == 2
            podname = plugin[0].strip()
            path = plugin[1].strip()
            podpath = File.expand_path("#{path}", file_abs_path)
            pods_array.push({:name => podname, :path => podpath});
         else
            puts "Invalid plugin specification: #{line}"
        end
    }
    return pods_array
end

def flutter_root(f)
    generated_xcode_build_settings = parse_KV_file(File.join(f, File.join('.ios', 'Flutter', 'Generated.xcconfig')))
    if generated_xcode_build_settings.empty?
        puts "Generated.xcconfig must exist. Make sure `flutter pub get` is executed in #{f}."
        exit
    end
    generated_xcode_build_settings.map { |p|
        if p[:name] == 'FLUTTER_ROOT'
            return p[:path]
        end
    }
end

# If this wasn't specified, assume it's two levels up from the directory of this script.
flutter_application_path ||= File.join(__dir__, '..', '..')
framework_dir = File.join(flutter_application_path, '.ios', 'Flutter')

engine_dir = File.join(framework_dir, 'engine')
if !File.exist?(engine_dir)
    # Copy the debug engine to have something to link against if the xcode backend script has not run yet.
    debug_framework_dir = File.join(flutter_root(flutter_application_path), 'bin', 'cache', 'artifacts', 'engine', 'ios')
    FileUtils.mkdir_p(engine_dir)
    FileUtils.cp_r(File.join(debug_framework_dir, 'Flutter.framework'), engine_dir)
    FileUtils.cp(File.join(debug_framework_dir, 'Flutter.podspec'), engine_dir)
end

pod 'Flutter', :path => engine_dir
pod 'FlutterPluginRegistrant', :path => File.join(framework_dir, 'FlutterPluginRegistrant')

symlinks_dir = File.join(framework_dir, '.symlinks')
FileUtils.mkdir_p(symlinks_dir)
plugin_pods = parse_KV_file(File.join(flutter_application_path, '.flutter-plugins'))
plugin_pods.map { |r|
    symlink = File.join(symlinks_dir, r[:name])
    FileUtils.rm_f(symlink)
    File.symlink(r[:path], symlink)
    pod r[:name], :path => File.join(symlink, 'ios')
}

# Ensure that ENABLE_BITCODE is set to NO, add a #include to Generated.xcconfig, and
# add a run script to the Build Phases.
post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            config.build_settings['ENABLE_BITCODE'] = 'NO'
            next if  config.base_configuration_reference == nil
            xcconfig_path = config.base_configuration_reference.real_path
            File.open(xcconfig_path, 'a+') do |file|
                file.puts "#include \"#{File.realpath(File.join(framework_dir, 'Generated.xcconfig'))}\""
            end
        end
    end
end

2.使App依赖于Flutter模块

假定的文件夹结构如下:

some/path/
  my_flutter/
    lib/main.dart
    .ios/
  MyApp/
    MyApp/
      AppDelegate.h
      AppDelegate.m (or swift)
      :

将Flutter应用程序添加到Podfile

集成Flutter框架需要使用CocoaPods依赖项管理器。这是因为Flutter框架也需要可用于您可能包含在my_flutter中的任何Flutter插件。

.将以下行添加到您的Podfile文件

flutter_application_path = 'path/to/my_flutter/'
eval(File.read(File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')), binding)

其中'path/to/my_flutter/'是你刚才生成的my_flutter文件路径

flutter_application_path = '../my_flutter'
eval(File.read(File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')), binding)

其中'flutter_module'是你刚才生成的flutter项目名

.运行

 pod install

注:如果报错

[!] InvalidPodfilefile: No such file or directory @ rb_sysopen - ./my_flutter/.ios/Flutter/podhelper.rb.

需要在my_flutter文件夹下执行以下flutter run,把.ios和.android等flutter配置生成出来
当你更改Flutter与app项目依赖关系的时候需要进入cd到my_flutter文件然后运行 flutter packages get从而刷新podhelper.rb脚本,然后cd进入MyApp文件运行pod install来刷新pod数据

.禁用目标的Bitcode

因为Flutter现在不支持bitcode。您需要禁用ENABLE_BITCODE位于目标

Build Settings->Build Options->Enable Bitcode = NO

.添加构建阶段以构建Dart代码

MyApp在“项目”导航器中选择顶级项目。在主视图的左侧选择TARGET MyApp,然后选择Build Phases选项卡。单击+主视图左上角的添加新构建阶段。选择New Run Script Phase。展开新的Run Script,只是附加到阶段列表。

将以下内容粘贴到Shell字段正下方的文本区域中:

"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" build
"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" embed

APP
ViewController.m:

#import <Flutter/Flutter.h>
- (void)rightBtnAction
{
    FlutterViewController *flutterPresentVC = [[FlutterViewController alloc] init];
    [self presentViewController:flutterPresentVC animated:false completion:nil];
}

1、创建 .bash_profile

(1) 启动终端

(2) 进入当前用户的home目录(默认就是):

        cd ~   或 cd /Users/YourMacUserName  

(3)输入

touch .bash_profile

2、查看 、编辑 .bash_profile 文件

(1)终端输入

open -e .bash_profile 

(如果只是查看,直接使用open .bash_profile)

(2)编辑

(3)关闭即可保存修改

3、更新刚配置的环境变量

输入

source .bash_profile
上一篇下一篇

猜你喜欢

热点阅读