Flutter与iOS混编(pod)

2018-11-16  本文已影响682人  ElaineYin

一、配置过程

// flutter_application_path:flutter工程的绝对路径
flutter_application_path = '**/**/my_flutter'
eval(File.read(File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')), binding)
image.png image.png

ok,run,如果运行报错,把Enable Bitcode 设为NO,Flutter混合开发还不支持bit code,所以在iOS工程检查项目并关闭bit code

image.png

现在应该可以运行起来了,如果还是不行,重复以上步骤

混编开始:
修改AppDelegate.h、AppDelegate.m

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

@interface AppDelegate : FlutterAppDelegate
@end

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

#include "AppDelegate.h"

@implementation AppDelegate

// This override can be omitted if you do not have any Flutter Plugins.
- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [GeneratedPluginRegistrant registerWithRegistry:self];
  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

@end

ViewController跳转

#import <Flutter/Flutter.h>
#import "ViewController.h"

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button addTarget:self
               action:@selector(handleButtonAction)
     forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"Press me" forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor blueColor]];
    button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
    [self.view addSubview:button];
}

- (void)handleButtonAction {
    FlutterViewController* flutterViewController = [[FlutterViewController alloc] init];
    [self presentViewController:flutterViewController animated:false completion:nil];
}
@end

二、热更新 Hot Reload

cd到flutter工程路径,执行flutter attach
attach成功之后,运行xcode,更新直接press 'r'

image.png

在VStudio中修改dart文件,press 'r'直接可以看到修改之后的显示

官网链接:Hot reload https://flutter.io/docs/development/tools/hot-reload

上一篇 下一篇

猜你喜欢

热点阅读