iOS現(xiàn)有項目集成Flutter

一、創(chuàng)建Flutter模塊

1.cd ~/Work/Projects
2.flutter create -t module flutter_module

image.png

二、創(chuàng)建依賴

1.通過CocoaPods管理依賴庫,直接在 iOS 工程的Podfile輸入

platform:ios,'9.0'
target 'FlutterModuleiOS' do

use_frameworks!

end

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

2.遇到的問題

image.png

flutter_application_path = '../flutter_module' //寫入的模塊名稱不正確

3. Enable Bitcode為NO

image.png

4. 在Xcode工程中添加Dart代碼的build phase

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

5.遇到的問題

image.png

image.png

三、在iOS項目中,通過FlutterViewController跳轉(zhuǎn)至Flutter頁面

AppDelegate.h/m
AppDelegate.h

#import <UIKit/UIKit.h>
#import <Flutter/Flutter.h>

@interface AppDelegate : FlutterAppDelegate

@end

AppDelegate.m

#import "AppDelegate.h"

@interface AppDelegate () @end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

@end

ViewController.h/m
ViewController.m

#import "ViewController.h"
#import <Flutter/Flutter.h>
#import <FlutterPluginRegistrant/GeneratedPluginRegistrant.h> // Only if you have Flutter Plugins

@interface ViewController ()

@end

@implementation ViewController
    
- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button addTarget:self action:@selector(handleButtonAction)
     forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"點擊" forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor redColor]];
    button.frame = CGRectMake(30, 0, [UIScreen mainScreen].bounds.size.width - 60, 40.0);
    button.center = self.view.center;
    [self.view addSubview:button];
}
    
- (void)handleButtonAction {
    FlutterViewController *flutterViewController = [[FlutterViewController alloc] init];
    flutterViewController.view.backgroundColor = [UIColor cyanColor];
    [flutterViewController setInitialRoute:@"route1"];
    
    [GeneratedPluginRegistrant registerWithRegistry:flutterViewController];
    
    [self presentViewController:flutterViewController animated:YES completion:nil];
}

@end
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容