原创 2017-06-28
关键点
- Swift依赖第三方Objective-C Framework、Library创建Pod
- Swift与Objective-C代码混合创建Pod
- Swift与OC、第三方Framework/Library混合创建Pod
在我的另一篇文章中介绍到了如何创建自己的公有、私有Pod,我以为创建Pod的坑就可以很简单的跳过,但是事情不是这样子的,在使用组件化开发私有组件的时候我又遇到了新问题(由于项目采用的是Objective-C开发):
- 业务组件依赖其他第三方厂家提供的OC Frameworwk、Library,这些第三方只提供了OC工具包,这个时候我们应该怎样让他集成到我们自己的其中一个业务组建中?
- 在私有业务组件开发中,遇到了项目急需演示验收,这个时候你并没有足够的时间采用Swift来实现你的业务(已经实现了一部分),但是在以前的OC开发项目中,你已经实现了此业务另一部分,现在你需要做的是把它们拿过来集成到你现在开发的一个业务组件中来 ,但是如何集成呢?
下面来说一说解决办法:
准备工作
在这里我们需要有一个Development Pod让我们来进行开发、测试,首先我们通过pod lib 来创建一个Pod,使其包含Example,Pods等,当然你也可以自己手动完成这一系列事情。
pod lib create Verify-SwiftOC3rd
Cloning `https://github.com/CocoaPods/pod-template.git` into `Verify-SwiftOC3rd`.
Configuring Verify-SwiftOC3rd template.
------------------------------
To get you started we need to ask a few questions, this should only take a minute.
If this is your first time we recommend running through with the guide:
- http://guides.cocoapods.org/making/using-pod-lib-create.html
( hold cmd and click links to open in a browser. )
What language do you want to use?? [ Swift / ObjC ]
> Swift
Would you like to include a demo application with your library? [ Yes / No ]
> Yes
Which testing frameworks will you use? [ Quick / None ]
> None
Would you like to do view based testing? [ Yes / No ]
> No
Running pod install on your new library.
上述所有的步骤你只需要按照提示信息完成即可,创建成功后将会自动打开项目,最后你看到的将会是:
Swift与OC互相调用流程
二是Swift只支持动态库(并非完全意义的动态库),而我们代码在Pod之后实际上是一个动态的Framework,Swift是有命名空间的一个概念,这时候你需要做的是在引用时需要写明命名空间。基于上述两点,我们只能在OC的.m文件中引用并且加上命名空间:
- 最后我们在Example目录下
Pod install
,在项目中引用Family,然后来验证我们这个Pod是否创建成功
Swift与OC 代码、 Framework、Library混合创建Pod
上述两个分别讲解了Swift与OC代码混合创建Pod,Swift与OC Framework、Library混合创建Pod,现在我们来将三者混合创建,这里也就是我们解决了三种:
- Swift引用第三方Framework、Library
- Swift与Objective-C 在Pod相互调用
- Swift与Objective-C 在项目、Pod相互调用Framework、Library、代码
依照上述两个章节所讲,我的.podspec如下:
Pod::Spec.new do |s|
s.name = 'Verify-SwiftOC3rd'
s.version = '0.1.4'
s.summary = '验证私有组件中使用Swift结合OC第三方.framework、.library库及OC文件组合打包构建私有Pod'
s.homepage = 'https://github.com/ApterKingRepo/Verify-SwiftOC3rd'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'ApterKing' => 'wangcccong@outlook.com' }
s.source = { :git => 'https://github.com/ApterKingRepo/Verify-SwiftOC3rd.git', :tag => s.version.to_s }
s.ios.deployment_target = '8.0'
s.source_files = 'Verify-SwiftOC3rd/Classes/**/*', 'Verify-SwiftOC3rd/Vendors/*.framework/Headers/**.h'
s.public_header_files = 'Verify-SwiftOC3rd/Classes/**/*.h', 'Verify-SwiftOC3rd/Vendors/BadiduMapAPI_Base.framework/Headers/BMKBaseComponent.h', 'Verify-SwiftOC3rd/Vendors/BadiduMapAPI_Map.framework/Headers/BMKMapComponent.h'
s.resources = 'Verify-SwiftOC3rd/Vendors/BaiduMapAPI_Map.framework/Resources/mapapi.bundle'
#s.module_name = 'BaiduMapKit'
s.frameworks = 'CoreLocation', 'QuartzCore', 'OpenGLES', 'SystemConfiguration', 'CoreGraphics', 'Security', 'CoreTelephony'
s.libraries = 'sqlite3.0', 'stdc++.6.0.9'
s.vendored_frameworks = 'Verify-SwiftOC3rd/Vendors/*.framework'
s.vendored_libraries = 'Verify-SwiftOC3rd/Vendors/thirdlibs/*.a'
s.preserve_paths = 'Verify-SwiftOC3rd/Vendors/*.framework', 'Verify-SwiftOC3rd/Vendors/thirdlibs/*.a'
s.pod_target_xcconfig = {
#'FRAMEWORK_SEARCH_PATHS' => '${PODS_ROOT}/Verify-SwiftOC3rd/Vendors',
'HEADER_SEARCH_PATHS' => '$(PODS_ROOT)/Verify-SwiftOC3rd/Vendors/*.framework/Headers',
'LD_RUNPATH_SEARCH_PATHS' => '$(PODS_ROOT)/Verify-SwiftOC3rd/Vendors/',
'OTHER_LDFLAGS' => '-ObjC'
}
s.prepare_command = <<-EOF
# 业务Module
rm -rf Verify-SwiftOC3rd/Classes/Modules
mkdir Verify-SwiftOC3rd/Classes/Modules
touch Verify-SwiftOC3rd/Classes/Modules/module.modulemap
cat <<-EOF > Verify-SwiftOC3rd/Classes/Modules/module.modulemap
framework module Verify {
umbrella header "Verify_SwiftOC3rd.h"
export *
link "sqlite3.0"
link "stdc++.6.0.9"
}
\EOF
# 创建Base Module
rm -rf Verify-SwiftOC3rd/Vendors/BaiduMapAPI_Base.framework/Modules
mkdir Verify-SwiftOC3rd/Vendors/BaiduMapAPI_Base.framework/Modules
touch Verify-SwiftOC3rd/Vendors/BaiduMapAPI_Base.framework/Modules/module.modulemap
cat <<-EOF > Verify-SwiftOC3rd/Vendors/BaiduMapAPI_Base.framework/Modules/module.modulemap
framework module BaiduMapAPI_Base {
umbrella header "BMKBaseComponent.h"
export *
link "sqlite3.0"
link "stdc++.6.0.9"
}
\EOF
# 创建Map Module
rm -rf Verify-SwiftOC3rd/Vendors/BaiduMapAPI_Map.framework/Modules
mkdir Verify-SwiftOC3rd/Vendors/BaiduMapAPI_Map.framework/Modules
touch Verify-SwiftOC3rd/Vendors/BaiduMapAPI_Map.framework/Modules/module.modulemap
cat <<-EOF > Verify-SwiftOC3rd/Vendors/BaiduMapAPI_Map.framework/Modules/module.modulemap
framework module BaiduMapAPI_Map {
umbrella header "BMKMapComponent.h"
export *
link "sqlite3.0"
link "stdc++.6.0.9"
}
\EOF
EOF
end
运行结果如下:
我将代码上传到了Verify-SwiftOC3rd,欢迎指正
网友评论