美文网首页Flutter学习
Flutter iOS从Bundle中加载代码

Flutter iOS从Bundle中加载代码

作者: 三三At你 | 来源:发表于2019-10-15 20:13 被阅读0次

0x0 使用自定义bundle

调用 -[FlutterViewController initWithProject:nibName:bundle:]传入自定义bundle
使用FlutterAppDelegate的话可以用分类覆盖initWithCoder:

@implementation FlutterViewController (Custom)
- (instancetype)initWithCoder:(NSCoder *)coder
{
    //这里传入bundle
    NSString *path = [NSString stringWithFormat:@"%@/FlutterAsset.bundle",[[NSBundle mainBundle] bundlePath]];
    NSBundle *bundle = [NSBundle bundleWithPath:path];
    id proj = [[FlutterDartProject alloc] initWithPrecompiledDartBundle:bundle];
    return [self initWithProject:proj nibName:nil bundle:nil];
}
@end

Bundle中的InfoPlist要一个Key FLTAssetsPath Value为自定义flutter_assets的路径
如flutter_assets
否则会去mainBundle中查找Frameworks/App.framework/flutter_assets
最终修改settings.application_kernel_asset

bundle结构如下


image.png

中间还有个条件!flutter::DartVM::IsRunningPrecompiledCode(),看起来是保证编译过程中不会修改路径

demo工程https://github.com/Kila2/flutter_custom_asset_path_example

相关文章

网友评论

    本文标题:Flutter iOS从Bundle中加载代码

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