当前位置:   article > 正文

ios 老项目接入flutter_flutterappdelegate

flutterappdelegate

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

添加flutter库

第一步

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

flutter create --template module flutter_module

第二步

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

  1. use_frameworks!
  2. source 'https://github.com/CocoaPods/Specs.git'
  3. platform :ios, '10.0'
  4. #flutter模块路径
  5. flutter_application_path = '../flutter_module'
  6. load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
  7. target 'Appmix' do
  8. pod 'AFNetworking'
  9. install_all_flutter_pods(flutter_application_path)
  10. end

第三步

pod install  

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

 

跳转flutter页面

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

已FlutterAppDelegate为例

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

  1. //AppDelegate.h
  2. #import <UIKit/UIKit.h>
  3. @import Flutter;
  4. @interface AppDelegate : FlutterAppDelegate
  5. @property (nonatomic,strong) FlutterEngine *flutterEngine;
  6. @end
  7. //AppDelegate.m
  8. #import "AppDelegate.h"
  9. #import <FlutterPluginRegistrant/GeneratedPluginRegistrant.h> // Used to connect plugins.
  10. @interface AppDelegate ()
  11. @end
  12. @implementation AppDelegate
  13. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  14. self.flutterEngine = [[FlutterEngine alloc] initWithName:@"io.flutter" project:nil];
  15. [self.flutterEngine runWithEntrypoint:nil];
  16. [GeneratedPluginRegistrant registerWithRegistry:self.flutterEngine];
  17. return [super application:application didFinishLaunchingWithOptions:launchOptions];
  18. }
  19. @end

跳转到flutter页面。

  1. -(IBAction)showFlutter{
  2. FlutterEngine *flutterEngine =
  3. ((AppDelegate *)UIApplication.sharedApplication.delegate).flutterEngine;
  4. FlutterViewController *flutterViewController =
  5. [[FlutterViewController alloc] initWithEngine:flutterEngine nibName:nil bundle:nil];
  6. flutterViewController.modalPresentationStyle = UIModalPresentationFullScreen;
  7. [self presentViewController:flutterViewController animated:YES completion:nil];
  8. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/145313
推荐阅读
相关标签
  

闽ICP备14008679号