ios 老项目接入flutter

在已有ios项目中添加flutter模块,在网上找了许多文章,跟着文章一步步操作没有成功。后来在官方英文文档中找到答案。官方文档官方文档

添加flutter库

第一步

在与ios项目同级的文件下,创建一个flutter model。

flutter create --template module flutter_module

第二步

在podfile文件中,添加如下代码

use_frameworks!
source 'https://github.com/CocoaPods/Specs.git' 
platform :ios, '10.0'

#flutter模块路径
flutter_application_path = '../flutter_module'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')

target 'Appmix' do
    
    pod 'AFNetworking'

    install_all_flutter_pods(flutter_application_path)

end

第三步

pod install  

看到pod中有Flutter的相关framework就成功了。

 

跳转flutter页面

官方提供了两种,一种是AppDelegate继承FlutterAppDelegate来实现,另一种通过AppDelegate实现FlutterAppLifeCycleProvider协议。官方文档

已FlutterAppDelegate为例

现在appdelegate文件中添加如下代码

//AppDelegate.h
#import <UIKit/UIKit.h>
@import Flutter;

@interface AppDelegate :  FlutterAppDelegate 

@property (nonatomic,strong) FlutterEngine *flutterEngine;
@end


//AppDelegate.m

#import "AppDelegate.h"
#import <FlutterPluginRegistrant/GeneratedPluginRegistrant.h> // Used to connect plugins.
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.flutterEngine = [[FlutterEngine alloc] initWithName:@"io.flutter" project:nil];
    [self.flutterEngine runWithEntrypoint:nil];
    [GeneratedPluginRegistrant registerWithRegistry:self.flutterEngine];

    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}


@end

跳转到flutter页面。

-(IBAction)showFlutter{
    FlutterEngine *flutterEngine =
        ((AppDelegate *)UIApplication.sharedApplication.delegate).flutterEngine;
    FlutterViewController *flutterViewController =
        [[FlutterViewController alloc] initWithEngine:flutterEngine nibName:nil bundle:nil];

    flutterViewController.modalPresentationStyle = UIModalPresentationFullScreen;
    
    [self presentViewController:flutterViewController animated:YES completion:nil];
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值