iOS 现有项目集成Flutter方式

Framework集成

创建Flutter项目和包装它的Pod库

flutter create -t module EWTFlutter
cd EWTFlutter
pod lib create EWTFlutter_iOS
vi buildFramework.sh

buildFrame.sh内容

if [ -z $out ]; then
    out='Frameworks'
fi
# 获取脚本执行目录
basepath=$(
    cd $(dirname $0)
    pwd
)
echo "准备输出所有文件到目录: $out"

echo "清除所有已编译文件"
find . -d -name build | xargs rm -rf
flutter clean
rm -rf $out
rm -rf build

flutter packages get

echo "编译flutter"
flutter build ios --debug --no-codesign
#release下放开下一行注释,注释掉上一行代码
# flutter build ios --release --no-codesign
echo "编译flutter完成"
mkdir $out
cp -r build/ios/Debug-iphoneos/*/*.framework $out
#release下放开下一行注释,注释掉上一行代码
# cp -r build/ios/Release-iphoneos/*/*.framework $out
cp -r .ios/Flutter/App.framework $out
cp -r .ios/Flutter/engine/Flutter.framework $out

echo "复制framework库到临时文件夹: $out"

frameworkPath=$basepath/EWTFlutter_iOS/EWTFlutter_iOS/$out

rm -rf "$frameworkPath"
mkdir $frameworkPath
cp -r $out/*.framework $basepath/EWTFlutter_iOS/EWTFlutter_iOS/Frameworks
rm -rf $out

echo "复制库文件到: $frameworkPath"

编译并打包

sh buildFramework.sh

配置并运行项目

添加以下内容

# 配置EWTFlutter_iOS.podspec
s.vendored_frameworks = "EWTFlutter_iOS/Frameworks/*.framework"
# 更新 pod
cd EWTFlutter_iOS/Example
pod install

EWTAppelegate

@import UIKit;
#import 

@interface EWTAppDelegate : FlutterAppDelegate
@end
    
#import "EWTAppDelegate.h"
#import "EWTViewController.h"

@implementation EWTAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.window setBackgroundColor:[UIColor whiteColor]];
    
    EWTViewController *con = [[EWTViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:con];
    [self.window setRootViewController:nav];
    [self.window makeKeyAndVisible];
    return YES;
}
@end

EWTViewController.m

#import "EWTViewController.h"
#import 

@interface EWTViewController ()

@end

@implementation EWTViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(100, 100, 200, 50)];
    [button setBackgroundColor:[UIColor lightGrayColor]];
    [button setTitle:@"ClickMePushToFlutterVC" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(btn_click) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}
- (void)btn_click {
    FlutterViewController *flutterViewController = [[FlutterViewController alloc] init];
    flutterViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"push" style:UIBarButtonItemStyleDone target:self action:@selector(btn_click)];
    [self.navigationController pushViewController:flutterViewController animated:YES];
    /* 方式 2
    FlutterViewController *fluvc = [[FlutterViewController alloc]init];
    [self addChildViewController:fluvc];
    fluvc.view.frame = self.view.bounds;
    [fluvc didMoveToParentViewController:self];
    [self.view addSubview:fluvc.view];
    [self.navigationController pushViewController:fluvc animated:YES];
     */
}
@end

源码集成

更改Podfile

use_frameworks!
# Flutter
 flutter_application_path = '../../'
 load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')

platform :ios, '8.0'

target 'EWTFlutter_iOS_Example' do
#  pod 'EWTFlutter_iOS', :path => '../'
  # Flutter
   install_all_flutter_pods(flutter_application_path)
  target 'EWTFlutter_iOS_Tests' do
    inherit! :search_paths

    #pod 'FBSnapshotTestCase'
  end
end

执行 pod install,运行项目

依附应用

cd EWTFlutter
flutter attach --app-id org.cocoapods.demo.EWTFlutter-iOS-Example

你可能感兴趣的:(iOS 现有项目集成Flutter方式)