Commit ef1f8f2d authored by 余玠's avatar 余玠

实现通过FlutterBoost销毁engine

parent aa8d47d6
......@@ -9,7 +9,7 @@
#import <UIKit/UIKit.h>
#import <flutter_boost/FlutterBoost.h>
@interface AppDelegate : FLBFlutterAppDelegate <UIApplicationDelegate>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
......@@ -19,12 +19,6 @@
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
PlatformRouterImp *router = [PlatformRouterImp new];
[FlutterBoostPlugin.sharedInstance startFlutterWithPlatform:router
onStart:^(FlutterEngine *engine) {
}];
self.window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen].bounds];
......@@ -35,17 +29,14 @@
vc.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"hybrid" image:nil tag:0];
FLBFlutterViewContainer *fvc = FLBFlutterViewContainer.new;
[fvc setName:@"tab" params:@{}];
UIViewController *fvc = UIViewController.new;
// [fvc setName:@"tab" params:@{}];
fvc.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"flutter_tab" image:nil tag:1];
UITabBarController *tabVC = [[UITabBarController alloc] init];
UINavigationController *rvc = [[UINavigationController alloc] initWithRootViewController:tabVC];
router.navigationController = rvc;
tabVC.viewControllers = @[vc,fvc];
self.window.rootViewController = rvc;
......@@ -57,16 +48,48 @@
UIButton *nativeButton = [UIButton buttonWithType:UIButtonTypeCustom];
nativeButton.frame = CGRectMake(self.window.frame.size.width * 0.5 - 50, 200, 100, 45);
nativeButton.frame = CGRectMake(self.window.frame.size.width * 0.5 - 60, 150, 130, 45);
nativeButton.backgroundColor = [UIColor redColor];
[nativeButton setTitle:@"push native" forState:UIControlStateNormal];
[nativeButton addTarget:self action:@selector(pushNative) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:nativeButton];
UIButton *startEngine = [UIButton buttonWithType:UIButtonTypeCustom];
startEngine.frame = CGRectMake(self.window.frame.size.width * 0.5 - 60, 230, 130, 40);
startEngine.backgroundColor = [UIColor redColor];
[startEngine setTitle:@"start engine" forState:UIControlStateNormal];
[startEngine addTarget:self action:@selector(startFlutter:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:startEngine];
UIButton *destroyEngine = [UIButton buttonWithType:UIButtonTypeCustom];
destroyEngine.frame = CGRectMake(self.window.frame.size.width * 0.5 - 60, 300, 130, 40);
destroyEngine.backgroundColor = [UIColor redColor];
[destroyEngine setTitle:@"destroy engine" forState:UIControlStateNormal];
[destroyEngine addTarget:self action:@selector(destroyFlutter:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:destroyEngine];
return YES;
}
- (void)startFlutter:(id)sender {
PlatformRouterImp *router = [PlatformRouterImp new];
router.navigationController = (UINavigationController*)self.window.rootViewController;
[FlutterBoostPlugin.sharedInstance startFlutterWithPlatform:router
onStart:^(FlutterEngine *engine) {
}];
}
- (void)destroyFlutter:(id)sender {
//切记:在destroyEngine前务必将所有FlutterViewController及其子类的实例销毁。在这里是FLBFlutterViewContainer。否则会异常;以下是全部步骤
//1. 首先通过为所有FlutterPlugin的methodChannel属性设为nil来接触其与FlutterEngine的间接强引用
//2. 销毁所有的FlutterViewController实例,来解除其与FlutterEngine的强引用
//3. 调用FlutterBoostPlugin.destroyEngine函数来解除与FlutterEngine的强引用
[FlutterBoostPlugin.sharedInstance destroyEngine];
}
- (void)pushNative
{
UINavigationController *nvc = (id)self.window.rootViewController;
......
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
......
......@@ -108,4 +108,6 @@
exts:(NSDictionary *)exts
onPageFinished:(void (^)(NSDictionary *))resultCallback
completion:(void (^)(BOOL))completion;
- (void)destroyEngine;
@end
......@@ -118,8 +118,8 @@
__weak __typeof__(self) weakSelf = self;
dispatch_once(&onceToken, ^{
__strong __typeof__(weakSelf) self = weakSelf;
self.factory = FLBFactory.new;
self.application = [self->_factory createApplication:platform];
FLBFactory *factory = FLBFactory.new;
self.application = [factory createApplication:platform];
[self.application startFlutterWithPlatform:platform
onStart:callback];
});
......@@ -169,4 +169,9 @@
id<FLBFlutterApplicationInterface> app = [[FlutterBoostPlugin sharedInstance] application];
[app close:uniqueId result:resultData exts:exts completion:completion];
}
- (void)destroyEngine{
self.methodChannel = nil;
self.application = nil;
}
@end
......@@ -28,7 +28,6 @@
#import "FlutterBoostPlugin.h"
@interface FlutterBoostPlugin()
@property (nonatomic,strong) id<FLBFlutterApplicationInterface> application;
@property (nonatomic,strong) id<FLBAbstractFactory> factory;
@property (nonatomic,strong) FlutterMethodChannel *methodChannel;
@property (nonatomic,copy) NSString *fPageId;
@property (nonatomic,copy) NSString *fPagename;
......
......@@ -101,11 +101,6 @@
arguments:nil];
}
- (FlutterEngine *)engine
{
return _engine;
}
- (void)atacheToViewController:(FlutterViewController *)vc
{
if(_engine.viewController != vc){
......@@ -128,5 +123,15 @@
// [self detach];
}
- (void)dealloc{
[self.engine setViewController:nil];
[self.engine destroyContext];
__weak __typeof__(_engine) weak = _engine;
NSLog(@"Retain count is %ld", CFGetRetainCount((__bridge CFTypeRef)weak));
//// CFRelease((__bridge CFTypeRef)_engine);
// _engine = nil;
// NSLog(@"after Retain count is %ld", CFGetRetainCount((__bridge CFTypeRef)weak));
// NSLog(@"[DEBUG]---dealloc--");
}
@end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment