创建静态库方式
1. Xcode
自带项目模板
2. 使用CocoaPods
创建
使用CocoaPods
最大的好处是解决第三库问题。OC
的命名空间是硬伤,所以在开发SDK
时假如用的第三方库别人的App也用到了就会文件等冲突。也许全部重命名是个解决办法,但是一旦多了就比较麻烦了。
使用CocoaPods自动创建方式
1. 终端执行命令,以YMinSDK
为例:
pod lib create YMinSDK
2. 确认输入5个问题选项
第二个
demo application
请务必YES
,帮助极大!第三个问题是问用哪个测试框架,或者不用
第四个问题是问是否需要基于界面的测试,CocoaPods推荐的是FBSnapShotTestCase
详细文档参考:Using Pod Lib Create
3. 如果CocoaPods
环境没有问题的话应该已经创建成功以及目录:
4. 打开YMinSDK.podspec
文件编辑
Pod::Spec.new do |s|
s.name = 'YMinSDK'
s.version = '0.1.0'
s.summary = 'A short description of YMinSDK.'
# This description is used to generate tags and improve search results.
# * Think: What does it do? Why did you write it? What is the focus?
# * Try to keep it short, snappy and to the point.
# * Write the description between the DESC delimiters below.
# * Finally, don't worry about the indent, CocoaPods strips it!
s.description = <<-DESC
TODO: Add long description of the pod here.
DESC
s.homepage = 'https://github.com/andy90s/YMinSDK'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { '梁先华' => '909901234@qq.com' }
s.source = { :git => 'https://github.com/andy90s/YMinSDK.git', :tag => '0.1.0' }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '8.0'
s.source_files = 'YMinSDK/Classes/**/*'
# s.resource_bundles = {
# 'YMinSDK' => ['YMinSDK/Assets/*.png']
# }
s.public_header_files = 'YMinSDK/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
s.dependency 'AFNetworking'
需要注意的几个:
-
s.source
资源地址,可以填写本地git仓库地址(pod 创建的工程本身就在git的管理之中) -
s.source_files
类库的源文件位置 -
s.resource_bundles
资源文件 -
s.public_header_files
头文件 -
s.dependency
依赖库 -
s.source
写本地git仓库地址即可
5.到Classes
目录下创建测试类
17.7.11
补充,写其他项目遇到一个问题无论我怎么pod install
,demo工程始终无法引用自己的库文件,我是各种尝试:降cocoapods版本(1.3.2beta),路径修改,重新创建工程都无法解决。最后是想起demo中也是有podfile
文件,稍作修改解决问题!注意:需要在example
目录下再次执行pod install
或者pod update
,修改如下(注释掉部分):
# use_frameworks!
target 'TestLib_Example' do
pod 'TestLib', :path => '../'
# target 'TestLib_Tests' do
# inherit! :search_paths
# end
end
6.到Example
文件夹下执行pod install
每次测试如果引入了第三方的依赖或者修改了Classes下面的代码都需要pod install
或者pod update
到demo中测试:
7.提交代码
为了方便我是直接使用Github Desktop
创建仓库并提交,推到GitHub
之后,可以看到readme
的模板内容
8.验证
到项目目录下
pod lib lint
这里我遇到了一个错误验证没有通过,提示我public_header_files
没有找到匹配的头文件,仔细检查了下发现默认是Pod/Classes/**/*.h
。同理如果遇到source_files
路径错误大家仔细检查下是否正确。
验证通过提示passed validation
,忽略警告加上--allow-warnings
。
9.使用CocoaPods-Packager
打包
sudo gem install cocoapods-packager
- 打包lib
注意需要到包含YMinSDK.podspec
文件的根目录下执行命令:
默认打包成.framework
pod package YMinSDK.podspec --force
打包.a
pod package YMinSDK.podspec --library --force
关于两者区别
打包成功会出现个YMinSDK-0.1.0
文件夹,这就是cocoapods
帮我们打包完毕文件存放位置,找到ios
文件,就可以看到打包好的.framework
文件。
至此打包完成。
目前遇到的问题与解决办法:
1. 写代码的时候提示各种not found xxx
到demo目录下执行 pod install
2. 资源访问问题
这个不能用mainbundle
,下面可参考
+ (NSString*)pathForFilename:(NSString*)filename pod:(NSString*)podName
{
NSString* bundlePath = [self bundlePathForPod:podName];
if (!bundlePath) { return nil; }
NSBundle* bundle = [NSBundle bundleWithPath:bundlePath];
NSString* extension = [filename pathExtension];
NSString* withoutExtension = [[filename lastPathComponent] stringByDeletingPathExtension];
NSString* path = [bundle pathForResource:withoutExtension ofType:extension];
return path;
}
+ (NSString*)bundlePathForPod:(NSString*)podName
{
// search all bundles
for (NSBundle* bundle in [NSBundle allBundles]) {
NSString* bundlePath = [bundle pathForResource:podName ofType:@"bundle"];
if (bundlePath) { return bundlePath; }
}
3. 打包之前一定要git commit
确定好提交的版本号,提交到仓库。实例:
git add .
git commit -a -m '0.1.0'
git tag -a 0.1.0 -m '0.1.0'
做完这些再去打包静态库。(注意podsepc中的版本号一定要一致)
4. 注意第三方库引用其他的第三方以及系统库
应该把所有需要的库都在podspec文件中的s.dependency
标识出来,如果少了库打出来的包会报错。
5. 打包生成的新podspec文件需要修改
s.ios.vendored_framework = 'xxx.framework'
注意默认路径是ios/xxx.framework
把ios路径去掉
补上s.dependency
、s.frameworks
等依赖(自动生成的podsepc文件没有)
6. 应该需要两个git仓库
一个用于存放你开发库的源代码仓库(私有)
一个用于存放打包出来的静态库和podspec文件的仓库(可公开/私有)
其他人引用静态库用的是第二个仓库地址
7. 在开发过程中,每引用第三方库最好先检查第三放库的podspec文件,检查所有的依赖,并打包测试。
8. 测试打出的静态库
每次打包上传仓库之后,应该在本地清理pod缓存 再pod install
- 缓存信息
pod cache list
- 清理所有
pod cache clean --all
- 清理指定
pod cache clean xxx
9.如果时间充足人手足够,最好还是自己实现第三方的功能。
待补充...