iOS Native混编Flutter交互实践

开篇

图形性能对比.jpg

开局一张图,其余全靠_?ios

目前flutter框架还比较新,又是谷歌家的东西,因此网上的文章基本都是讲安卓和flutter混合开发的,没有iOS和Flutter混合开发的比较详细的步骤实操。git

混编的前提是你的电脑必须有flutter环境,不会配的请先谷歌教程配置完后再来查看此混编教程。github

正题

由于本文要讲的是iOS,因此正常状况下的环境是macOS + Xcode + flutter环境(v0.8.2-beta);再加上flutter 须要的的dart语言编辑器 Android Studio 、IntelliJ IDEA 或 Visual Studio Code (VS Code) ;由于flutter是多平台,因此也要安装安卓相关的SDK。objective-c

本教程是基于flutter环境版本v0.8.2-betaxcode

环境配好后,命令行输入:flutter doctor -v , 确保FlutterAndroid toolchainiOS toolchainConnected devices (链接中的设备,这个列表是你打开你Xcode虚拟机或者安卓虚拟机的时候才会有) 都不是[✗]这个符号,则说明你的环境OK了 【也要注意编辑器的flutter环境】app

flutter doctor.png

Xcode工程项目配置

Xcode项目的开始

最权威的教程固然是flutter自家的混编wiki,iOS部分我英文理解能力不是很好,实际操做的时候也按照教程操做了一遍,再和网上教程总结了一遍,一路踏坑而出。框架

  • Flutter混合开发还不支持bit code,因此在iOS工程检查项目并关闭bit code

关闭项目bitcode.png

  • flutter module建立 (不要耦合近Xcode项目中,最好放到与项目目录同级)

这里由于使用的是flutter环境(v0.8.2-beta),应该也是很新的分支。看网上说明flutter的master才是最新的分支。先用beta建立module,若是建立不成功再切换master分支进行建立async

create flutter module.png

目录结构.png

若是建立不成功,请切换master分支试一下;执行flutter channel master编辑器

  • flutter module 重要文件分析 (部分是隐藏文件,记得查看所有)

flutter module文件分析.png

flutter内部文件分析.png

  • 建立Xcode项目中的Config文件,引向flutter moduleide

    新建Config目录,管理Xcode工程的配置衔接文件,分别建立 Flutter.xcconfigDebug.xcconfigRelease.xcconfig 三个配置文件;其中Flutter.xcconfig 是指向外目录flutter module的Generated.xcconfig 文件路径引用文件,其余两个表明Xcode的环境配置文件。

新建胶水配置文件.png

  • 三个文件的引入内容 (所引用的都是绝对路径,最终都是指引到Generated.xcconfig

In Flutter.xcconfig:

#include "../../flutter_module/.ios/Flutter/Generated.xcconfig"
ENABLE_BITCODE=NO
复制代码

In Debug.xcconfig:

#include "Flutter.xcconfig"
复制代码

In Release.xcconfig:

#include "Flutter.xcconfig"
FLUTTER_BUILD_MODE=release
复制代码

这里有个值得注意的地方,若是你是使用的pod管理你的项目,则Debug.xcconfigRelease.xcconfig 都须要添加一行pod的引用

  • Xcode project环境配置选择

Xcode 项目配置选择.png

  • 最重要: 引入xcode-backend.sh

在iOS工程里添加运行脚本 "$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" build,而且确保Run Script这一行在"Target dependencies" 或者 "Check Pods Manifest.lock"后面。

引入xcode-backend.png

此时点击Xcode的运行,会执行到xcode-backend.sh脚本;此时在iOS工程目录,也会生成一个Flutter文件夹,里面是Flutter工程的产物(这个就是flutter最终与Native交互的产物)

  • 添加flutter编译产物

右键项目 - Add Files to 'xxx' 【Options先选Create groups】,选择Flutter目录

添加flutter编译产物.png

可是flutter_assets 并不能使用Create groups 的方式添加,只能使用Creat folder references 的方式添加进Xcode项目内,不然跳转flutter会页面渲染失败(页面空白)。

flutter_assets特殊处理1.png
应该删除 flutter_assets,文件夹再 Add Files to 'xxx',选择 Creat folder references ;最终以下图

flutter_assets特殊处理.png

将iOS工程目录下的Flutter文件夹添加到工程,而后确保文件夹下的两个framework添加到Embeded Binaries

flutter framework引入.png

至此,Xcode与Flutter之间混编配置完成,两个项目文件已经关联上了。这时候你就能够修改main.dart 文件内容,从新编译运行Xcode 则APP.framework自动会被编译成最新的flutter代码。

项目中使用pod管理状况

1、旧项目没使用pod管理,混编后又想pod管理

  • 一、先删除Xcode工程项目中的Run Script
  • 二、pod init
  • 三、在生成的pod file文件写你要增长的第三方框架,如pod 'AFNetworking’
  • 四、pod install
  • 五、(使用.xcworkspace打开项目)从新配置Run Script
  • 六、修改Debug.xcconfigRelease。xcconfig 都须要增长一行pod config文件引用:(本身查看本身的Pods目录文件路径, release就使用.release.xcconfig)
#include "Flutter.xcconfig"
// 下面为pod引入须要增长的一行
#include "Pods/Target Support Files/Pods-iOSBridgeFlutterDemo/Pods-iOSBridgeFlutterDemo.debug.xcconfig"
复制代码
  • 七、项目从新编译,Success

2、若是旧项目已经使用pod管理

  • 若是项目ignore Pods文件夹, 则走一方法中的一、四、五、六、7步骤
  • 若是项目Pods文件夹存在,则走一方法中的六、7步骤

PS: 每次pod update或者pod install都会报错,由于Run Script的缘由,因此每次添加或更新pod都得删除Run Script更新pod再添加回Run Script(一、四、六、7步骤);这些繁琐的操做不知道有没有办法避免,知道的朋友能够回复一下?不吝赐教,谢谢!

Xcode 与 Flutter 交互

AppDelegate 改造

改造AppDelegate.h,使其继承FlutterAppDelegate:

#import <Flutter/Flutter.h>

@interface AppDelegate : FlutterAppDelegate <UIApplicationDelegate, FlutterAppLifeCycleProvider>

@end
复制代码

改造AppDelegate.m

#import "AppDelegate.h"

@interface AppDelegate ()
    
@end

@implementation AppDelegate
{
  FlutterPluginAppLifeCycleDelegate *_lifeCycleDelegate;
}
    
- (instancetype)init {
    if (self = [super init]) {
        _lifeCycleDelegate = [[FlutterPluginAppLifeCycleDelegate alloc] init];
    }
    return self;
}
    
- (BOOL)application:(UIApplication*)application
didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
    return [_lifeCycleDelegate application:application didFinishLaunchingWithOptions:launchOptions];
}
    
- (void)applicationDidEnterBackground:(UIApplication*)application {
    [_lifeCycleDelegate applicationDidEnterBackground:application];
}
    
- (void)applicationWillEnterForeground:(UIApplication*)application {
    [_lifeCycleDelegate applicationWillEnterForeground:application];
}
    
- (void)applicationWillResignActive:(UIApplication*)application {
    [_lifeCycleDelegate applicationWillResignActive:application];
}
    
- (void)applicationDidBecomeActive:(UIApplication*)application {
    [_lifeCycleDelegate applicationDidBecomeActive:application];
}
    
- (void)applicationWillTerminate:(UIApplication*)application {
    [_lifeCycleDelegate applicationWillTerminate:application];
}
    
- (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];
}

#pragma mark - Flutter
    // 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];
    }
}

@end

复制代码

Flutter主动,Native被动 (MethodChannel)

Flutter 代码: 引入import 'package:flutter/services.dart';

请用下面代码替换class _MyHomePageState extends State<MyHomePage> 这个类内容

class _MyHomePageState extends State<MyHomePage> {

  // 建立一个给native的channel (相似iOS的通知)
  static const methodChannel = const MethodChannel('com.pages.your/native_get');

  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;

      print('flutter的log打印:如今输出count=$_counter');
      // 当个数累积到3的时候给客户端发参数
      if(_counter == 3) {
        _toNativeSomethingAndGetInfo();
      }

      // 当个数累积到5的时候给客户端发参数
      if(_counter == 1002) {
        Map<String, String> map = { "title": "这是一条来自flutter的参数" };
        methodChannel.invokeMethod('toNativePush',map);
      }

      // 当个数累积到8的时候给客户端发参数
      if(_counter == 1005) {
        Map<String, dynamic> map = { "content": "flutterPop回来","data":[1,2,3,4,5]};
        methodChannel.invokeMethod('toNativePop',map);
      }
    });
  }

  // 给客户端发送一些东东 , 而且拿到一些东东
  Future<Null> _toNativeSomethingAndGetInfo() async {
    dynamic result;
    try {
      result = await methodChannel.invokeMethod('toNativeSomething','大佬你点击了$_counter下');
    } on PlatformException {
      result = 100000;
    }
    setState(() {
      // 类型判断
      if (result is int) {
        _counter = result;
      }

    });
  }

  @override
  Widget build(BuildContext context) {

    return new Scaffold(
// appBar: new AppBar(
// // Here we take the value from the MyHomePage object that was created by
// // the App.build method, and use it to set our appbar title.
// title: new Text(widget.title),
// ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Text(
              'You have pushed the button this many times:',
            ),
            new Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: new Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

复制代码

Native 代码:

- (void)pushFlutterViewController {
        FlutterViewController* flutterViewController = [[FlutterViewController alloc] initWithProject:nil nibName:nil bundle:nil];
        flutterViewController.navigationItem.title = @"Flutter Demo";
            __weak __typeof(self) weakSelf = self;
        
        // 要与main.dart中一致
        NSString *channelName = @"com.pages.your/native_get";
        
            FlutterMethodChannel *messageChannel = [FlutterMethodChannel methodChannelWithName:channelName binaryMessenger:flutterViewController];
        
            [messageChannel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult  _Nonnull result) {
                // call.method 获取 flutter 给回到的方法名,要匹配到 channelName 对应的多个 发送方法名,通常须要判断区分
                // call.arguments 获取到 flutter 给到的参数,(好比跳转到另外一个页面所须要参数)
                // result 是给flutter的回调, 该回调只能使用一次
                NSLog(@"flutter 给到我:\nmethod=%@ \narguments = %@",call.method,call.arguments);
                
                if ([call.method isEqualToString:@"toNativeSomething"]) {
                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"flutter回调" message:[NSString stringWithFormat:@"%@",call.arguments] delegate:self cancelButtonTitle:@"肯定" otherButtonTitles:nil];
                    [alertView show];
                    
                    // 回调给flutter
                    if (result) {
                        result(@1000);
                    }
                } else if ([call.method isEqualToString:@"toNativePush"]) {
                    ThirdViewController *testVC = [[ThirdViewController alloc] init];
                    testVC.parames = call.arguments;
                    [weakSelf.navigationController pushViewController:testVC animated:YES];
                } else if ([call.method isEqualToString:@"toNativePop"]) {
                    [weakSelf.navigationController popViewControllerAnimated:YES];
                }
            }];
        
        [self.navigationController pushViewController:flutterViewController animated:YES];
    }
复制代码

Native主动,Flutter被动 (EventChannel)

通常用于flutter初始化须要从客户端获取一些参数做为渲染条件;相似iOS这边的KVO,监听flutter是否已经在监听,监听的时候回调到代理【这步其实仍是flutter监听的时候,内部发了一个通知,iOS这边收到并回调】,iOS Native处理代理,并回调给flutter所须要参数

Flutter 代码 (class中):

// 注册一个通知
  static const EventChannel eventChannel = const EventChannel('com.pages.your/native_post');

  // 渲染前的操做,相似viewDidLoad
  @override
  void initState() {
    super.initState();
    
    // 监听事件,同时发送参数12345
    eventChannel.receiveBroadcastStream(12345).listen(_onEvent,onError: _onError);
  }

  String naviTitle = '你好,大佬' ;
  // 回调事件
  void _onEvent(Object event) {
    setState(() {
      naviTitle =  event.toString();
    });
  }
  // 错误返回
  void _onError(Object error) {

  }
复制代码

Native代码:

- (void)pushFlutterViewController_EventChannel {
        FlutterViewController* flutterViewController = [[FlutterViewController alloc] initWithProject:nil nibName:nil bundle:nil];
        flutterViewController.navigationItem.title = @"EventChannel Demo";
        // 要与main.dart中一致
        NSString *channelName = @"com.pages.your/native_post";
        
        FlutterEventChannel *evenChannal = [FlutterEventChannel eventChannelWithName:channelName binaryMessenger:flutterViewController];
        // 代理
        [evenChannal setStreamHandler:self];
        
        [self.navigationController pushViewController:flutterViewController animated:YES];
    }

#pragma mark - <FlutterStreamHandler>
    // // 这个onListen是Flutter端开始监听这个channel时的回调,第二个参数 EventSink是用来传数据的载体。
    - (FlutterError* _Nullable)onListenWithArguments:(id _Nullable)arguments
                                           eventSink:(FlutterEventSink)events {
        
        // arguments flutter给native的参数
        // 回调给flutter, 建议使用实例指向,由于该block可使用屡次
        if (events) {
            events(@"我是标题");
        }
        return nil;
    }
    
    /// flutter再也不接收
    - (FlutterError* _Nullable)onCancelWithArguments:(id _Nullable)arguments {
        // arguments flutter给native的参数
        return nil;
 }
 
复制代码

交互总结

这两种方式都差很少,一个使用的时候使用block,一个使用的时候使用delegate;最终回调给flutter的都是经过block。

MethodChannel 使用block,上下文更加明确;同一个channel name能够根据flutter给回的call.methodcall.arguments更加灵活的处理回调handle, 回调只能使用一次(意思就是就算你建立一个实例指向block,单block回调只能使用一次,回调以后flutter block那边再也不接收);

EventChannel 使用delegate,代码层次更鲜明;同一个channel name只能经过判断arguments参数处理回调handle, 回调可使用屡次(建立一个实例指向block,该block能够向flutter发送屡次);

BasicMessageChannel 请自行学习。

疑问

建立使用FlutterViewController Xcode的Memory一直在增长到一个水平,分类重写- (void)dealloc 也没有进来,估计是内存泄漏了。因而去查了官方的Issues ,确实有几个关联:

Native 简单push FlutterViewControllerpop回,内存到达一个阶段后不降,FlutterViewController不会执行dealloc方法。不知道这谁知道有没有解决方案?不吝赐教,谢谢!

学习

相关文章
相关标签/搜索