美文网首页
Flutter探究二-OC与Flutter混编

Flutter探究二-OC与Flutter混编

作者: superFool | 来源:发表于2021-02-19 08:29 被阅读0次

    Flutter探究二

    [toc]

    一、将 Flutter module 集成到 iOS 项目(混编)

    1.制作 Flutter module

    为了将 Flutter 集成到你的既有应用里,第一步要创建一个 Flutter module。把flutter作为一个组件集成到现有的项目中
    在命令行中执行:

    cd some/path/ --Flutter组件存放的目录
    flutter create --template module my_flutter
    

    Flutter module 会创建在 some/path/my_flutter/ 目录。在这个目录中,你可以像在其它 Flutter 项目中一样,执行 flutter 命令。比如 flutter run --debug 或者 flutter build ios。

    模块组织
    在 my_flutter 模块,目录结构和普通 Flutter 应用类似:

    my_flutter/
    ├── .ios/
    │   ├── Runner.xcworkspace
    │   └── Flutter/podhelper.rb
    ├── lib/
    │   └── main.dart
    ├── test/
    └── pubspec.yaml
    

    添加你的 Dart 代码到 lib/ 目录。
    添加 Flutter 依赖到 my_flutter/pubspec.yaml,包括 Flutter packages 和 plugins。
    .ios/ 隐藏文件夹包含了一个 Xcode workspace,用于单独运行你的 Flutter module。它是一个独立启动 Flutter 代码的壳工程,并且包含了一个帮助脚本,用于编译 framewroks 或者使用 CocoaPods 将 Flutter module 集成到你的既有应用。

    注意:

    • iOS 代码要添加到你的既有应用或者 Flutter plugin中,而不是 Flutter module 的 .ios/ 目录下。 .ios/ 下的改变不会集成到你的既有应用,并且这有可能被 Flutter 重写。

    • 由于 .ios/ 目录是自动生成的,因此请勿对其进行版本控制(git忽略该文件夹)。在新机器上构建 module 时,请在使用 Flutter module 构建 iOS 项目之前,先于 my_flutter 目录运行 flutter pub get 以生成 .ios/ 目录。

    2.集成 Flutter module

    有两种集成的方式(CocoaPods,手动拖到项目里)这里我们用Pods集成,使用 Flutter 会 增加应用体积 。
    只需要在 Xcode 中编译应用,就可以自动运行脚本来集成 dart 代码和 plugin。这个方法允许你使用 Flutter module 中的最新代码快速迭代开发,而无需在 Xcode 以外执行额外的命令。

    下面的示例假设你的既有应用和 Flutter module 在相邻目录。如果你有不同的目录结构,需要适配到对应的路径。

    some/path/
    ├── my_flutter/
    │   └── .ios/
    │       └── Flutter/
    │         └── podhelper.rb
    └── MyApp/
        └── Podfile
    

    1.在 Podfile 中添加下面代码:

    flutter_application_path = '../my_flutter' 
    load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
    

    2.Podfile的target中添加下面代码:

    target 'MyApp' do
      install_all_flutter_pods(flutter_application_path)
    end
    

    3.运行 pod instal
    注意:
    当你在 my_flutter/pubspec.yaml 改变了 Flutter plugin 依赖,需要在 Flutter module 目录运行 flutter pub get,来更新会被podhelper.rb 脚本用到的 plugin 列表,然后再次在你的应用目录 some/path/MyApp 运行 pod install.

    在 Xcode 中打开 MyApp.xcworkspace ,你现在可以使用 ⌘B 编译项目了。

    3.在 iOS 应用中添加 Flutter 页面

    1.启动 FlutterEngine 和 FlutterViewController
    FlutterEngine 充当 Dart VM 和 Flutter 运行时的主机; FlutterViewController 依附于 FlutterEngine,给 Flutter 传递 UIKit 的输入事件,并展示被 FlutterEngine 渲染的每一帧画面。

    创建一个 FlutterEngine
    创建 FlutterEngine 的合适位置取决于您的应用。作为示例,我们将在应用启动的 app delegate 中创建一个 FlutterEngine,并作为属性暴露给外界。

    @import UIKit;
    @import Flutter;
    
    @interface AppDelegate : FlutterAppDelegate //这里的集成不是必须的
    @property (nonatomic,strong) FlutterEngine *flutterEngine;
    @end
    

    在 AppDelegate.m:

    #import <FlutterPluginRegistrant/GeneratedPluginRegistrant.h>
    
    #import "AppDelegate.h"
    
    @implementation AppDelegate
    
    - (BOOL)application:(UIApplication *)application
        didFinishLaunchingWithOptions:(NSDictionary<UIApplicationLaunchOptionsKey, id> *)launchOptions {
      self.flutterEngine = [[FlutterEngine alloc] initWithName:@"my flutter engine"];
      // Runs the default Dart entrypoint with a default Flutter route.
      [self.flutterEngine run];
      // Used to connect plugins (only if you have plugins with iOS platform code).
      [GeneratedPluginRegistrant registerWithRegistry:self.flutterEngine];
      return [super application:application didFinishLaunchingWithOptions:launchOptions];
    }
    

    使用 FlutterEngine 展示 FlutterViewController

    @import Flutter;
    #import "AppDelegate.h"
    #import "ViewController.h"
    
    @implementation ViewController
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        // Make a button to call the showFlutter function when pressed.
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button addTarget:self
                   action:@selector(showFlutter)
         forControlEvents:UIControlEventTouchUpInside];
        [button setTitle:@"Show Flutter!" forState:UIControlStateNormal];
        button.backgroundColor = UIColor.blueColor;
        button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
        [self.view addSubview:button];
    }
    
    - (void)showFlutter {
        FlutterEngine *flutterEngine =
            ((AppDelegate *)UIApplication.sharedApplication.delegate).flutterEngine;
        FlutterViewController *flutterViewController =
            [[FlutterViewController alloc] initWithEngine:flutterEngine nibName:nil bundle:nil];
        [self presentViewController:flutterViewController animated:YES completion:nil];
    }
    @end
    

    使用 FlutterAppDelegate
    推荐让你应用的 UIApplicationDelegate 继承 FlutterAppDelegate,但不是必须的。

    FlutterAppDelegate 有这些功能:

    • 传递应用的回调,例如 openURL 到 Flutter 的插件 —— local_auth。
    • 传递状态栏点击(这只能在 AppDelegate 中检测)到 Flutter 的点击置顶行为。

    如果你的 app delegate 不能直接继承 FlutterAppDelegate,让你的 app delegate 实现 FlutterAppLifeCycleProvider 协议,来确保 Flutter plugins 接收到必要的回调。否则,依赖这些事件的 plugins 将会有无法预估的行为。
    例如:

    @import Flutter;
    @import UIKit;
    @import FlutterPluginRegistrant;
    
    @interface AppDelegate : UIResponder <UIApplicationDelegate, FlutterAppLifeCycleProvider>
    @property (strong, nonatomic) UIWindow *window;
    @property (nonatomic,strong) FlutterEngine *flutterEngine;
    @end
    

    在具体实现中,应该最大化地委托给 FlutterPluginAppLifeCycleDelegate:

    @interface AppDelegate ()
    @property (nonatomic, strong) FlutterPluginAppLifeCycleDelegate* lifeCycleDelegate;
    @end
    
    @implementation AppDelegate
    
    - (instancetype)init {
        if (self = [super init]) {
            _lifeCycleDelegate = [[FlutterPluginAppLifeCycleDelegate alloc] init];
        }
        return self;
    }
    
    - (BOOL)application:(UIApplication*)application
    didFinishLaunchingWithOptions:(NSDictionary<UIApplicationLaunchOptionsKey, id>*))launchOptions {
        self.flutterEngine = [[FlutterEngine alloc] initWithName:@"io.flutter" project:nil];
        [self.flutterEngine runWithEntrypoint:nil];
        [GeneratedPluginRegistrant registerWithRegistry:self.flutterEngine];
        return [_lifeCycleDelegate application:application didFinishLaunchingWithOptions:launchOptions];
    }
    
    // Returns the key window's rootViewController, if it's a FlutterViewController.
    // Otherwise, returns nil.
    - (FlutterViewController*)rootFlutterViewController {
        UIViewController* viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
        if ([viewController isKindOfClass:[FlutterViewController class]]) {
            return (FlutterViewController*)viewController;
        }
        return nil;
    }
    
    - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
        [super touchesBegan:touches withEvent:event];
    
        // Pass status bar taps to key window Flutter rootViewController.
        if (self.rootFlutterViewController != nil) {
            [self.rootFlutterViewController handleStatusBarTouches:event];
        }
    }
    
    - (void)application:(UIApplication*)application
    didRegisterUserNotificationSettings:(UIUserNotificationSettings*)notificationSettings {
        [_lifeCycleDelegate application:application
    didRegisterUserNotificationSettings:notificationSettings];
    }
    
    - (void)application:(UIApplication*)application
    didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
        [_lifeCycleDelegate application:application
    didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
    }
    
    - (void)application:(UIApplication*)application
    didReceiveRemoteNotification:(NSDictionary*)userInfo
    fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
        [_lifeCycleDelegate application:application
           didReceiveRemoteNotification:userInfo
                 fetchCompletionHandler:completionHandler];
    }
    
    - (BOOL)application:(UIApplication*)application
                openURL:(NSURL*)url
                options:(NSDictionary<UIApplicationOpenURLOptionsKey, id>*)options {
        return [_lifeCycleDelegate application:application openURL:url options:options];
    }
    
    - (BOOL)application:(UIApplication*)application handleOpenURL:(NSURL*)url {
        return [_lifeCycleDelegate application:application handleOpenURL:url];
    }
    
    - (BOOL)application:(UIApplication*)application
                openURL:(NSURL*)url
      sourceApplication:(NSString*)sourceApplication
             annotation:(id)annotation {
        return [_lifeCycleDelegate application:application
                                       openURL:url
                             sourceApplication:sourceApplication
                                    annotation:annotation];
    }
    
    - (void)application:(UIApplication*)application
    performActionForShortcutItem:(UIApplicationShortcutItem*)shortcutItem
      completionHandler:(void (^)(BOOL succeeded))completionHandler NS_AVAILABLE_IOS(9_0) {
        [_lifeCycleDelegate application:application
           performActionForShortcutItem:shortcutItem
                      completionHandler:completionHandler];
    }
    
    - (void)application:(UIApplication*)application
    handleEventsForBackgroundURLSession:(nonnull NSString*)identifier
      completionHandler:(nonnull void (^)(void))completionHandler {
        [_lifeCycleDelegate application:application
    handleEventsForBackgroundURLSession:identifier
                      completionHandler:completionHandler];
    }
    
    - (void)application:(UIApplication*)application
    performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
        [_lifeCycleDelegate application:application performFetchWithCompletionHandler:completionHandler];
    }
    
    - (void)addApplicationLifeCycleDelegate:(NSObject<FlutterPlugin>*)delegate {
        [_lifeCycleDelegate addDelegate:delegate];
    }
    @end
    

    二、dart 简单介绍

    2.1变量声明

    1.var

    类似于JavaScript中的var,它可以接收任何类型的变量,但最大的不同是Dart中var变量一旦赋值,类型便会确定,则不能再改变其类型,如:

    var t;
    t = "hi world";
    // 下面代码在dart中会报错,因为变量t的类型已经确定为String,
    // 类型一旦确定后则不能再更改其类型。
    t = 1000;
    

    2.dynamic和Object

    Object 是Dart所有对象的根基类,也就是说所有类型都是Object的子类(包括Function和Null),所以任何类型的数据都可以赋值给Object声明的对象. dynamic与var一样都是关键词,声明的变量可以赋值任意对象。 而dynamic与Object相同之处在于,他们声明的变量可以在后期改变赋值类型。

    dynamic t;
     Object x;
     t = "hi world";
     x = 'Hello Object';
     //下面代码没有问题
     t = 1000;
     x = 1000;
    

    dynamic与Object不同的是,dynamic声明的对象编译器会提供所有可能的组合, 而Object声明的对象只能使用Object的属性与方法, 否则编译器会报错。如:

    dynamic a;
     Object b;
     main() {
         a = "";
         b = "";
         printLengths();
     }   
    
     printLengths() {
         // no warning
         print(a.length);
         // warning:
         // The getter 'length' is not defined for the class 'Object'
         print(b.length);
     }
    

    变量a不会报错, 变量b编译器会报错

    dynamic的这个特性与Objective-C中的id作用很像. dynamic的这个特点使得我们在使用它时需要格外注意,这很容易引入一个运行时错误.

    3.final和const

    如果您从未打算更改一个变量,那么使用 final 或 const,不是var,也不是一个类型。 一个 final 变量只能被设置一次,两者区别在于:const 变量是一个编译时常量,final变量在第一次使用时被初始化。被final或者const修饰的变量,变量类型可以省略,如:

    //可以省略String这个类型声明
    final str = "hi world";
    //final String str = "hi world"; 
    const str1 = "hi world";
    //const String str1 = "hi world";
    

    2.2函数

    Dart是一种真正的面向对象的语言,所以即使是函数也是对象,并且有一个类型Function。这意味着函数可以赋值给变量或作为参数传递给其他函数,这是函数式编程的典型特征。

    1.函数声明

    bool isNoble(int atomicNumber) {
      return _nobleGases[atomicNumber] != null;
    }
    

    Dart函数声明如果没有显式声明返回值类型时会默认当做dynamic处理,注意,函数返回值没有类型推断:

    typedef bool CALLBACK();
    
    //不指定返回类型,此时默认为dynamic,不是bool
    isNoble(int atomicNumber) {
      return _nobleGases[atomicNumber] != null;
    }
    
    void test(CALLBACK cb){
       print(cb()); 
    }
    //报错,isNoble不是bool类型
    test(isNoble);
    

    2.对于只包含一个表达式的函数,可以使用简写语法

    bool isNoble (int atomicNumber)=> _nobleGases [ atomicNumber ] != null ;
    

    3.函数作为变量

    var say = (str){
      print(str);
    };
    say("hi world");
    

    4.函数作为参数传递

    void execute(var callback) {
        callback();
    }
    execute(() => print("xxx"))
    

    5.可选的位置参数

    包装一组函数参数,用[]标记为可选的位置参数,并放在参数列表的最后面:

    String say(String from, String msg, [String device]) {
      var result = '$from says $msg';
      if (device != null) {
        result = '$result with a $device';
      }
      return result;
    }
    

    例子:

    say('Bob', 'Howdy'); //结果是: Bob says Howdy
    say('Bob', 'Howdy', 'smoke signal'); //结果是:Bob says Howdy with a smoke signal
    

    6.可选的命名参数

    定义函数时,使用{param1, param2, …},放在参数列表的最后面,用于指定命名参数。例如:

    //设置[bold]和[hidden]标志
    void enableFlags({bool bold, bool hidden}) {
        // ... 
    }
    //调用函数时,可以使用指定命名参数。例如:paramName: value
    enableFlags(bold: true, hidden: false);
    

    注意,不能同时使用可选的位置参数和可选的命名参数

    2.3异步支持

    1.Future

    Future表示一个异步操作的最终完成(或失败)及其结果值的表示。简单来说,它就是用于处理异步操作的,异步处理成功了就执行成功的操作,异步处理失败了就捕获错误或者停止后续操作。一个Future只会对应一个结果,要么成功,要么失败。Future 的所有API的返回值仍然是一个Future对象,所以可以很方便的进行链式调用。

    Future.then
    为了方便示例,在本例中我们使用Future.delayed 创建了一个延时任务(实际场景会是一个真正的耗时任务,比如一次网络请求),即2秒后返回结果字符串"hi world!",然后我们在then中接收异步结果并打印结果,代码如下:

    Future.delayed(new Duration(seconds: 2),(){
       return "hi world!";
    }).then((data){
       print(data);
    });
    

    Future.catchError
    如果异步任务发生错误,我们可以在catchError中捕获错误,我们将上面示例改为:

    Future.delayed(new Duration(seconds: 2),(){
       //return "hi world!";
       throw AssertionError("Error");
    }).then((data){
       //执行成功会走到这里 
       print(data);
    }).catchError((e){
       //执行失败会走到这里   
       print(e);
    }).whenComplete((){
       //无论成功或失败都会走到这里
    });
    

    then方法还有一个可选参数onError,我们也可以它来捕获异常:

    Future.delayed(new Duration(seconds: 2), () {
        //return "hi world!";
        throw AssertionError("Error");
    }).then((data) {
        print("success");
    }, onError: (e) {
        print(e);
    });
    

    Future.wait
    有些时候,我们需要等待多个异步任务都执行结束后才进行一些操作可以使用Future.wait,它接受一个Future数组参数,只有数组中所有Future都执行成功后,才会触发then的成功回调,只要有一个Future执行失败,就会触发错误回调。

    Future.wait([
      // 2秒后返回结果  
      Future.delayed(new Duration(seconds: 2), () {
        return "hello";
      }),
      // 4秒后返回结果  
      Future.delayed(new Duration(seconds: 4), () {
        return " world";
      })
    ]).then((results){
      print(results[0]+results[1]);
    }).catchError((e){
      print(e);
    });
    

    Async/await
    Dart中的async/await 和JavaScript中的async/await功能和用法是一模一样的,为了解决回调地狱问题

    回调地狱(Callback Hell)
    如果代码中有大量异步逻辑,并且出现大量异步任务依赖其它异步任务的结果时,必然会出现Future.then回调中套回调情况。举个例子,比如现在有个需求场景是用户先登录,登录成功后会获得用户ID,然后通过用户ID,再去请求用户个人信息,获取到用户个人信息后,为了使用方便,我们需要将其缓存在本地文件系统,代码如下:
    //先分别定义各个异步任务

    Future<String> login(String userName, String pwd){
        ...
        //用户登录
    };
    Future<String> getUserInfo(String id){
        ...
        //获取用户信息 
    };
    Future saveUserInfo(String userInfo){
        ...
        // 保存用户信息 
    };
    

    接下来,执行整个任务流:

    login("alice","******").then((id){
     //登录成功后通过,id获取用户信息    
     getUserInfo(id).then((userInfo){
        //获取用户信息后保存 
        saveUserInfo(userInfo).then((){
           //保存用户信息,接下来执行其它操作
            ...
        });
      });
    })
    

    目前我们项目中应该也存在回调七八层的情况
    使用Future消除Callback Hell

    login("alice","******").then((id){
          return getUserInfo(id);
    }).then((userInfo){
        return saveUserInfo(userInfo);
    }).then((e){
       //执行接下来的操作 
    }).catchError((e){
      //错误处理  
      print(e);
    });
    

    “Future 的所有API的返回值仍然是一个Future对象,所以可以很方便的进行链式调用” ,如果在then中返回的是一个Future的话,该future会执行,执行结束后会触发后面的then回调,这样依次向下,就避免了层层嵌套。

    使用async/await消除callback hell
    通过Future回调中再返回Future的方式虽然能避免层层嵌套,但是还是有一层回调,有没有一种方式能够让我们可以像写同步代码那样来执行异步任务而不使用回调的方式?答案是肯定的,这就要使用async/await了,下面我们先直接看代码,然后再解释,代码如下:

    task() async {
       try{
        String id = await login("alice","******");
        String userInfo = await getUserInfo(id);
        await saveUserInfo(userInfo);
        //执行接下来的操作   
       } catch(e){
        //错误处理   
        print(e);   
       }  
    }
    
    • async用来表示函数是异步的,定义的函数会返回一个Future对象,可以使用then方法添加回调函数。
    • await 后面是一个Future,表示等待该异步任务完成,异步完成后才会往下走;await必须出现在 async 函数内部。

    其实,无论是在JavaScript还是Dart中,async/await都只是一个语法糖,编译器或解释器最终都会将其转化为一个Promise(Future)的调用链。

    Stream
    Stream 也是用于接收异步事件数据,和Future 不同的是,它可以接收多个异步操作的结果(成功或失败)。 也就是说,在执行异步任务时,可以通过多次触发成功或失败事件来传递结果数据或错误异常。

    Stream.fromFutures([
      // 1秒后返回结果
      Future.delayed(new Duration(seconds: 1), () {
        return "hello 1";
      }),
      // 抛出一个异常
      Future.delayed(new Duration(seconds: 2),(){
        throw AssertionError("Error");
      }),
      // 3秒后返回结果
      Future.delayed(new Duration(seconds: 3), () {
        return "hello 3";
      })
    ]).listen((data){
       print(data);
    }, onError: (e){
       print(e.message);
    },onDone: (){
    
    });
    

    上面的代码依次会输出:

    I/flutter (17666): hello 1
    I/flutter (17666): Error
    I/flutter (17666): hello 3
    

    三、打包

    直接用对应的iOS工程打包

    四、常用第三方

    三方package获取
    flutter一些常用的插件库

    其他资料

    《flutter实战》

    相关文章

      网友评论

          本文标题:Flutter探究二-OC与Flutter混编

          本文链接:https://www.haomeiwen.com/subject/gqbaxltx.html