Flutter plugin - ios
2020-07-13 本文已影响0人
陈贤森
好多年前做过私有pod库的demo,之后再没接触,已然忘光。最近在做flutter plugin 的ios端开发,弄了一天多,记录一下:
1、podspec基本配置:
Flutter plugin 开发的核心在于pod的依赖文件:demo.podspec
下面是我这个plugin用到的podspec
文件:
#
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html.
# Run `pod lib lint card_ocr.podspec' to validate before publishing.
#
Pod::Spec.new do |s|
s.name = 'card_ocr'
s.version = '0.0.1'
s.summary = 'A Flutter plugin for iOS for scanning credit cards using the camera.'
s.description = <<-DESC
A Flutter plugin for iOS and Android for scanning credit cards using the camera.
DESC
s.homepage = 'http://example.com'
s.license = { :file => '../LICENSE' }
s.author = { 'Your Company' => 'email@example.com' }
s.info_plist = {
'CFBundleIdentifier' => 'com.zb.example'
}
s.source = { :path => '.' }
s.platform = :ios, '9.0'
s.source_files = 'CardIO/**/*','Classes/**/*'
s.public_header_files = 'Classes/*.h'
# s.resources = 'Assets/localization/*'
s.resource_bundle = { 'bundle' => 'Assets/localization/*' }
s.dependency 'Flutter'
s.dependency 'Localize-Swift'
s.vendored_libraries = 'CardIO/*.{a}'
s.frameworks = 'Accelerate','AVFoundation','AudioToolbox','CoreMedia','MobileCoreServices'
# s.pod_target_xcconfig = { 'OTHER_LDFLAGS' => '-ObjC' }
s.pod_target_xcconfig = { 'OTHER_LDFLAGS' => '-lc++'}
end
具体参数:
1.该参数是项目中所有用到的文件,*
通配符,**
目录递归通配,引用好像有顺序要求,CardIO
和Classes
反过来写,Classes
中的会报错找不到CardIO
中的类,下面的写法 就不报错
s.source_files = 'CardIO/**/*','Classes/**/*'
2.要暴露出去的头文件
s.public_header_files = 'Classes/*.h'
3.s.resources
是把目录下的资源文件打包到主工程的bundle目录下,s.resource_bundle
打包资源文件到自己的bundle中
# s.resources = 'Assets/localization/*'
s.resource_bundle = { 'bundle' => 'Assets/localization/*' }
4.项目中用到的三方静态库,加上这个之后就不用在build setting
的library search patch
中配置路径
s.vendored_libraries = 'CardIO/*.{a}'
5.项目所依赖的系统的framework,提前配置,不用单独在导入
s.frameworks = 'Accelerate','AVFoundation','AudioToolbox','CoreMedia','MobileCoreServices'
6.提前配置 other link flag
单个配置和多个配置:
# s.pod_target_xcconfig = { 'OTHER_LDFLAGS' => '-ObjC' }
s.pod_target_xcconfig = { 'OTHER_LDFLAGS' => '-lc++'}
#s.pod_target_xcconfig = { 'OTHER_LDFLAGS' => ['-ObjC','-lc++']}
2、framework中的bundle资源
1、不单独创建资源bundle
s.resources = 'Assets/localization/*'
........
//没有单独给资源文件创建bundle的,直接通过获取当点bundle(xxxxx)
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *iconPath = [bundle pathForResource:@"ic_close_dark" ofType:@"png"];
UIImage *icon1 = [UIImage imageWithContentsOfFile:iconPath];
2、单独创建资源bundle
s.resource_bundle = { 'flutterimageshow' => 'Assets/localization/*' }
.......
//先找到当前的类所在的framework,然后通过bundleName找到对应的资源bundle的URL,在找到对应的资源bundle
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSURL *url = [bundle URLForResource:@"flutterimageshow" withExtension:@"bundle"];
NSBundle *sourceBundle = [NSBundle bundleWithURL:url];
NSString *icon1Path = [sourceBundle pathForResource:@"ic_close_dark" ofType:@"png"];
UIImage *icon1 = [UIImage imageWithContentsOfFile:icon1Path];
以上是两种读取本地资源的方式。