具体如何创建插件依赖插件,可参考闲鱼技术
现在有一个调用阿里云上传图片的插件alioss,打开文件夹其中有个目录是ios,仔细查看有个alioss.podspec文件,用CocaPods创建过自己库的同学应该不陌生,文件内容如下:
#
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
s.name = 'alioss'
s.version = '0.0.1'
s.summary = 'A new Flutter plugin.'
s.description = <<-DESC
A new Flutter plugin.
DESC
s.homepage = 'http://example.com'
s.license = { :file => '../LICENSE' }
s.author = { 'Your Company' => 'email@example.com' }
s.source = { :path => '.' }
s.source_files = 'Classes/**/*'
s.public_header_files = 'Classes/**/*.h'
s.dependency 'Flutter'
s.ios.deployment_target = '8.0'
end
1.引用阿里三方库我们在倒数第二行添加。
s.dependency 'AliyunOSSiOS'
这样我们就引用了阿里的pod库了,封装好相关的上传代码,到flutter主工程下的ios目录下pod install 完成后,就可以测试上传文件的代码了。
2、引用三方静态库(.a文件)我们再添加一行。(在另一个插件里有个连接打印机功能,就不再重复.podspec文件了)
s.vendored_libraries = 'Classes/LPAPI/CAR_PRINT.a'
然后把静态库放在相应的文件夹路径下,同样使用pod install重新构建工程,run下会报警告报错,内容如下:
Warning: no rule to process file '/Users/xxx/development/FlutterPlugin/xxx/ios/Classes/LPAPI/CAR_PRINT.a' of type archive.ar for architecture armv7
Warning: no rule to process file '/Users/xxx/development/FlutterPlugin/xxx/ios/Classes/LPAPI/CAR_PRINT.a' of type archive.ar for architecture arm64
ld: library not found for -lCAR_PRINT
clang: error: linker command failed with exit code 1 (use -v to see invocation)
这时候需要删除 flutter主工程的pods下对应的插件(千万要找到引用.a对应的插件)的TARGETS-->Build Settings->Other Linker Flags列表中的-l"CAR_PRINT" 选项,再run一下还是报警告报错,内容如下:
Warning: no rule to process file '/Users/xxx/development/FlutterPlugin/xxx/ios/Classes/LPAPI/CAR_PRINT.a' of type archive.ar for architecture armv7
Warning: no rule to process file '/Users/xxx/development/FlutterPlugin/xxx/ios/Classes/LPAPI/CAR_PRINT.a' of type archive.ar for architecture arm64
Undefined symbols for architecture arm64:
"_OBJC_CLASS_$_LPAPI", referenced from:
objc-class-ref in PrinterViewController.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
说明PrinterViewController文件引用了CAR_PRINT.a的头文件。
这时候我们需要TARGETS-->Build Phases-->Compile Sources 删除CAR_PRINT.a文件,再在Compile Sources同级的另一个选项Link Binary With Libraries 里添加刚刚删除的CAR_PRINT.a文件。
最后再run一下,控制台显示内容如下:
Launching lib/main.dart on iPhone 8 in debug mode...
Xcode build done. 51.9s
最后我们还要根据引用.a静态库的提示是否要在插件的TARGETS-->Build Settings->Other Linker Flags列表中加入-ObjC或-all_load等其他flag.
如果上述步骤出错可以使用pod install重置设置选项,还有xcode还是无法运行flutter主工程的,我用的是vscode和vscode flutter插件来运行。
3、引用三方动态库Framework的坑还没试过,最后的资料里有提到。
最后我们来一张运行图纪念一下。