一 创建私有 pods
创建私有 Spec Repo(也就是所有私有 pod 的仓库)
spec repo
是pods
的一个索引,是所有公开的pods
的podspec
文件的一个仓库,其实就是一个部署在服务器的Git仓库,当你使用CocoaPods 后它会被Clone到本地的~/.cocoapods/repos
。这个仓库只存放podspec
文件
步骤1:创建私有仓库
1、在git上创建私有仓库地址
2、在终端terminal
执行命令
# pod repo add [Private Repo Name] [GitHub HTTPS clone URL]
$ pod repo add MySpecs https://git.net/winter/MySpecs.git
复制代码
步骤2:创建 pod 项目工程
1、创建 Pod 项目工程
# pod lib create [Project Name]
$ pod lib create MyLib
复制代码
然后按照步骤一步一步执行,如果碰到创建失败的情况,更新 cocoaPods 再试试!
2、添加相关代码
├── MyLib
│ ├── Assets **存放资源文件!!!**
│ └── Classes
│ └── ReplaceMe.m **注意存放你自己实现的库相关代码!!!**
复制代码
├── Example
│ **就是一个样例工程相关代码文件**
复制代码
3、开发模式下测试 pod 打开Example工程目录下的podfile
文件:
#pod 'MyLib', :path => '../' # 指定路径
pod 'MyLib', :path => '../MyLib.podspec' # 指定podspec文件
复制代码
然后在 Example 工程目录下执行 pod update
命令安装依赖,打开项目工程,可以看到库文件都被加载到Pods子项目中了 不过它们并没有在 Pods 目录下,而是跟测试项目一样存在于 Development Pods/MyLib 中,这是因为我们是在本地测试,而没有把 podspec 文件添加到 Spec Repo 中的缘故。测试库文件没有问题,接着我们需要执行第4步
4、提交Pod到代码仓库,注意:不是podspec
索引仓库,是代码仓库 在终端执行命令:
$ git add .
$ git commit -s -m "初始化MyLib 库"
$ git remote add origin [email protected]:winter/MyLib.git #添加远端仓库
$ git push origin master #提交到远端仓库
$ git tag -m "first release" "0.1.0" #打上标签,这个很重要
$ git push --tags #推送tag到远端仓库
复制代码
到这里,成功提交到远程代码仓库,MyLib Pod 库就初步完成了代码实现,接下来就是重点了,将制作的私有库放到podspec
索引仓库。
步骤3:提交podspec
文件到私有Spec Repo
仓库
1、配置podspec
文件
#
# Be sure to run `pod lib lint MyLib.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
s.name = 'MyLib'
s.version = '0.1.0'
s.summary = 'MyLib for example'
# 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 = 'http://www.jianshu.com/u/06f42a993882'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'winter' => '[email protected]' }
s.source = { :git => 'https://git.net/winter/MyLib.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '8.0'
s.source_files = 'MyLib/Classes/**/*'
s.resource_bundles = {
'MyLib' => ['MyLib/Assets/*.png']
}
# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
end
复制代码
打开MyLib
工程目录下的MyLib.podspec
文件并参考上面的说明配置好相关选项。 podspec
更多配置请参考: 官方文档
2、编辑完MyLib.podspec
文件后,需要验证一下这个MyLib.podspec
文件是否可用
$ pod lib lint
// 如果终端输出这个信息,就说明验证通过,否则会提示错误信息,去修改
-> MyLib (0.1.0)
MyLib passed validation.
复制代码
3、验证通过,想SpecRepo提交podspec
# pod repo push [Repo名] [podspec 文件名字]
$ pod repo push MySpecs MyLib.podspec
复制代码
如果提交成功 终端上就会输出一些相关pod信息,如果不成功,则会打印一些失败信息,切记:失败后,去解决问题,或者更新cocoaPods。
二 使用 cocoaPods
使用私有 pods
我们可以直接指定某一个依赖的podspec
,这样就可以使用公司的私有库。该方案有利于使企业内部的公共项目支持CocoaPods。
pod 'MySpec', :podspec => 'https://my.com/mySpec.podspec'
复制代码
在创建私有 pods 遇到引用第三方framework(例如umeng)
需要注意,如果 pod repo push
出现下面的错误信息:
can't get podspec validation - ERROR | [iOS] xcodebuild: Returned an unsuccessful exit code
复制代码
解决办法,可以试试:
1,pod lib lint --allow-warnings
2,解决所有的warning
3,pod repo push <repo-name> <podspec-file-name>.podspec --allow-warnings --use-libraries
复制代码
如果pod lib lint --allow-warnings
出现一些 not find file
之类的warning,一定要解决掉,否则也会出现以上错误信息。
不更新 Pods
CocoaPods 在执行pod instal
l和pod update
时,会默认先更新一次pod spec
索引,使用--no-repo-update
参数可以禁止其做索引更新操作。
pod install --no-repo-update
pod update --no-repo-update
复制代码
给 Pods 添加资源文件
podspec
里面
s.resource_bundles = {
'MyLibrary' => ['your/path/Assets/**/*.{png,xib,plist}']
}
复制代码
访问 bundle 资源文件
一般情况下,我们自己创建的pods
添加的资源文件,使用[NSBundle mainBundle]
是找不到该资源的路径,所以,在这里,我们需要创建一个NSBundle
的 category
。
@implementation NSBundle (MyLibrary)
+ (NSBundle *)my_myLibraryBundle {
return [self bundleWithURL:[self my_myLibraryBundleURL]];
}
+ (NSURL *)my_myLibraryBundleURL {
NSBundle *bundle = [NSBundle bundleForClass:[MYSomeClass class]];
return [bundle URLForResource:@"MyLibrary" withExtension:@"bundle"];
}
@end
复制代码
顺便说下,MYSomeClass
这个类可以是任意类名,但是有个前提,这个类必须是在你创建的library
或者framework
内。再说这个逻辑:先拿到最外面的 bundle,对 framework 链接方式来说是 framework 的 bundle 的根目录,对静态库链接方式来说就是 target client 的 main bundle,然后再去找下面名为MyLibrary
的 bundle。
图片资源的访问
上面我们已经可以正常访问我们自己的 bundle,如果访问我们自己 bundle 的图片资源,还是一样创建UIImage
的category
。
#import "UIImage+MyLibrary.h"
#import "NSBundle+MyLibrary.h"
@implementation UIImage (MyLibrary)
+ (UIImage *)my_bundleImageNamed:(NSString *)name {
return [self my_imageNamed:name inBundle:[NSBundle my_myLibraryBundle]];
}
+ (UIImage *)my_imageNamed:(NSString *)name inBundle:(NSBundle *)bundle {
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_8_0
return [UIImage imageNamed:name inBundle:bundle compatibleWithTraitCollection:nil];
#elif __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_8_0
return [UIImage imageWithContentsOfFile:[bundle pathForResource:name ofType:@"png"]];
#else
if ([UIImage respondsToSelector:@selector(imageNamed:inBundle:compatibleWithTraitCollection:)]) {
return [UIImage imageNamed:name inBundle:bundle compatibleWithTraitCollection:nil];
} else {
return [UIImage imageWithContentsOfFile:[bundle pathForResource:name ofType:@"png"]];
}
#endif
}
@end
复制代码
+ imageNamed:inBundle:compatibleWithTraitCollection:
这个方法iOS8以后才有,所以需要条件编译。+ imageWithContentsOfFile:
没有缓存机制。
问题 Unable to find a specification for xxxxx
有时候在安装某一第三方会出现 “Unable to find a specification for xxxxx
” 这个问题,在这里找到了解决方法,只需要把当前Pod的目录清理一下就行了。在终端执行以下命令:
pod repo remove master
pod setup
复制代码
setup(pod setup
可能需要花费很长时间)成功后执行install
或update
即可.